-
Notifications
You must be signed in to change notification settings - Fork 77
/
decimal.go
80 lines (70 loc) · 1.61 KB
/
decimal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package fixedn
import (
"errors"
"fmt"
"math/big"
"strconv"
"strings"
)
const maxAllowedPrecision = 16
// ErrInvalidFormat is returned when decimal format is invalid.
var ErrInvalidFormat = errors.New("invalid decimal format")
var _pow10 []*big.Int
func init() {
var p = int64(1)
for i := 0; i <= maxAllowedPrecision; i++ {
_pow10 = append(_pow10, big.NewInt(p))
p *= 10
}
}
func pow10(n int) *big.Int {
last := len(_pow10) - 1
if n <= last {
return _pow10[n]
}
p := new(big.Int)
p.Mul(_pow10[last], _pow10[1])
for i := last + 1; i < n; i++ {
p.Mul(p, _pow10[1])
}
return p
}
// ToString converts big decimal with specified precision to string.
func ToString(bi *big.Int, precision int) string {
var dp, fp big.Int
dp.QuoRem(bi, pow10(precision), &fp)
var s = dp.String()
if fp.Sign() == 0 {
return s
}
frac := fp.Uint64()
trimmed := 0
for ; frac%10 == 0; frac /= 10 {
trimmed++
}
return s + "." + fmt.Sprintf("%0"+strconv.FormatUint(uint64(precision-trimmed), 10)+"d", frac)
}
// FromString converts string to a big decimal with specified precision.
func FromString(s string, precision int) (*big.Int, error) {
parts := strings.SplitN(s, ".", 2)
bi, ok := new(big.Int).SetString(parts[0], 10)
if !ok {
return nil, ErrInvalidFormat
}
bi.Mul(bi, pow10(precision))
if len(parts) == 1 {
return bi, nil
}
if len(parts[1]) > precision {
return nil, ErrInvalidFormat
}
fp, ok := new(big.Int).SetString(parts[1], 10)
if !ok {
return nil, ErrInvalidFormat
}
fp.Mul(fp, pow10(precision-len(parts[1])))
if bi.Sign() == -1 {
return bi.Sub(bi, fp), nil
}
return bi.Add(bi, fp), nil
}