OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / common / size.go
1 package common
2
3 import (
4         "fmt"
5         "math/big"
6 )
7
8 // The different number of units
9 var (
10         Douglas  = BigPow(10, 42)
11         Einstein = BigPow(10, 21)
12         Ether    = BigPow(10, 18)
13         Finney   = BigPow(10, 15)
14         Szabo    = BigPow(10, 12)
15         Shannon  = BigPow(10, 9)
16         Babbage  = BigPow(10, 6)
17         Ada      = BigPow(10, 3)
18         Wei      = big.NewInt(1)
19 )
20
21 //
22 // Currency to string
23 // Returns a string representing a human readable format
24 func CurrencyToString(num *big.Int) string {
25         var (
26                 fin   *big.Int = num
27                 denom string   = "Wei"
28         )
29
30         switch {
31         case num.Cmp(Ether) >= 0:
32                 fin = new(big.Int).Div(num, Ether)
33                 denom = "Ether"
34         case num.Cmp(Finney) >= 0:
35                 fin = new(big.Int).Div(num, Finney)
36                 denom = "Finney"
37         case num.Cmp(Szabo) >= 0:
38                 fin = new(big.Int).Div(num, Szabo)
39                 denom = "Szabo"
40         case num.Cmp(Shannon) >= 0:
41                 fin = new(big.Int).Div(num, Shannon)
42                 denom = "Shannon"
43         case num.Cmp(Babbage) >= 0:
44                 fin = new(big.Int).Div(num, Babbage)
45                 denom = "Babbage"
46         case num.Cmp(Ada) >= 0:
47                 fin = new(big.Int).Div(num, Ada)
48                 denom = "Ada"
49         }
50
51         // TODO add comment clarifying expected behavior
52         if len(fin.String()) > 5 {
53                 return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
54         }
55
56         return fmt.Sprintf("%v %s", fin, denom)
57 }