github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/ethutil/common.go (about)

     1  package ethutil
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"runtime"
     7  )
     8  
     9  func IsWindows() bool {
    10  	return runtime.GOOS == "windows"
    11  }
    12  
    13  func WindonizePath(path string) string {
    14  	if string(path[0]) == "/" && IsWindows() {
    15  		path = path[1:]
    16  	}
    17  	return path
    18  }
    19  
    20  // The different number of units
    21  var (
    22  	Douglas  = BigPow(10, 42)
    23  	Einstein = BigPow(10, 21)
    24  	Ether    = BigPow(10, 18)
    25  	Finney   = BigPow(10, 15)
    26  	Szabo    = BigPow(10, 12)
    27  	Shannon  = BigPow(10, 9)
    28  	Babbage  = BigPow(10, 6)
    29  	Ada      = BigPow(10, 3)
    30  	Wei      = big.NewInt(1)
    31  )
    32  
    33  //
    34  // Currency to string
    35  // Returns a string representing a human readable format
    36  func CurrencyToString(num *big.Int) string {
    37  	var (
    38  		fin   *big.Int = num
    39  		denom string   = "Wei"
    40  	)
    41  
    42  	switch {
    43  	case num.Cmp(Douglas) >= 0:
    44  		fin = new(big.Int).Div(num, Douglas)
    45  		denom = "Douglas"
    46  	case num.Cmp(Einstein) >= 0:
    47  		fin = new(big.Int).Div(num, Einstein)
    48  		denom = "Einstein"
    49  	case num.Cmp(Ether) >= 0:
    50  		fin = new(big.Int).Div(num, Ether)
    51  		denom = "Ether"
    52  	case num.Cmp(Finney) >= 0:
    53  		fin = new(big.Int).Div(num, Finney)
    54  		denom = "Finney"
    55  	case num.Cmp(Szabo) >= 0:
    56  		fin = new(big.Int).Div(num, Szabo)
    57  		denom = "Szabo"
    58  	case num.Cmp(Shannon) >= 0:
    59  		fin = new(big.Int).Div(num, Shannon)
    60  		denom = "Shannon"
    61  	case num.Cmp(Babbage) >= 0:
    62  		fin = new(big.Int).Div(num, Babbage)
    63  		denom = "Babbage"
    64  	case num.Cmp(Ada) >= 0:
    65  		fin = new(big.Int).Div(num, Ada)
    66  		denom = "Ada"
    67  	}
    68  
    69  	// TODO add comment clarifying expected behavior
    70  	if len(fin.String()) > 5 {
    71  		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
    72  	}
    73  
    74  	return fmt.Sprintf("%v %s", fin, denom)
    75  }
    76  
    77  // Common big integers often used
    78  var (
    79  	Big1     = big.NewInt(1)
    80  	Big2     = big.NewInt(2)
    81  	Big3     = big.NewInt(3)
    82  	Big0     = big.NewInt(0)
    83  	BigTrue  = Big1
    84  	BigFalse = Big0
    85  	Big32    = big.NewInt(32)
    86  	Big256   = big.NewInt(0xff)
    87  	Big257   = big.NewInt(257)
    88  )