github.com/r8d8/go-ethereum@v5.5.2+incompatible/common/size.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package common
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  )
    23  
    24  type StorageSize float64
    25  
    26  func (self StorageSize) String() string {
    27  	if self > 1000000 {
    28  		return fmt.Sprintf("%.2f mB", self/1000000)
    29  	} else if self > 1000 {
    30  		return fmt.Sprintf("%.2f kB", self/1000)
    31  	} else {
    32  		return fmt.Sprintf("%.2f B", self)
    33  	}
    34  }
    35  
    36  func (self StorageSize) Int64() int64 {
    37  	return int64(self)
    38  }
    39  
    40  // The different number of units
    41  var (
    42  	Douglas  = BigPow(10, 42)
    43  	Einstein = BigPow(10, 21)
    44  	Ether    = BigPow(10, 18)
    45  	Finney   = BigPow(10, 15)
    46  	Szabo    = BigPow(10, 12)
    47  	Shannon  = BigPow(10, 9)
    48  	Babbage  = BigPow(10, 6)
    49  	Ada      = BigPow(10, 3)
    50  	Wei      = big.NewInt(1)
    51  )
    52  
    53  //
    54  // Currency to string
    55  // Returns a string representing a human readable format
    56  func CurrencyToString(num *big.Int) string {
    57  	var (
    58  		fin   *big.Int = num
    59  		denom string   = "Wei"
    60  	)
    61  
    62  	switch {
    63  	case num.Cmp(Ether) >= 0:
    64  		fin = new(big.Int).Div(num, Ether)
    65  		denom = "Ether"
    66  	case num.Cmp(Finney) >= 0:
    67  		fin = new(big.Int).Div(num, Finney)
    68  		denom = "Finney"
    69  	case num.Cmp(Szabo) >= 0:
    70  		fin = new(big.Int).Div(num, Szabo)
    71  		denom = "Szabo"
    72  	case num.Cmp(Shannon) >= 0:
    73  		fin = new(big.Int).Div(num, Shannon)
    74  		denom = "Shannon"
    75  	case num.Cmp(Babbage) >= 0:
    76  		fin = new(big.Int).Div(num, Babbage)
    77  		denom = "Babbage"
    78  	case num.Cmp(Ada) >= 0:
    79  		fin = new(big.Int).Div(num, Ada)
    80  		denom = "Ada"
    81  	}
    82  
    83  	// TODO add comment clarifying expected behavior
    84  	if len(fin.String()) > 5 {
    85  		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
    86  	}
    87  
    88  	return fmt.Sprintf("%v %s", fin, denom)
    89  }