-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecimal.go
More file actions
117 lines (90 loc) · 2.5 KB
/
Copy pathdecimal.go
File metadata and controls
117 lines (90 loc) · 2.5 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package number
import (
"github.com/shopspring/decimal"
)
const (
presentDecimals = 8
persistentDecimals = 32
)
type Decimal struct {
decimal.Decimal
}
func Zero() Decimal {
return Decimal{}
}
func NewDecimal(value int64, decimals int32) Decimal {
return Decimal{decimal.New(value, -decimals).Round(persistentDecimals)}
}
func FromString(source string) Decimal {
d := decimal.RequireFromString(source)
return Decimal{d.Round(persistentDecimals)}
}
func FromFloat(source float64) Decimal {
return Decimal{decimal.NewFromFloat(source).Round(persistentDecimals)}
}
func (d Decimal) Integer(precision uint8) Integer {
return Integer{d.Mul(NewDecimal(1, -int32(precision))).BigInt(), precision}
}
func (a Decimal) Add(b Decimal) Decimal {
return Decimal{a.Decimal.Add(b.Decimal)}
}
func (a Decimal) Sub(b Decimal) Decimal {
return Decimal{a.Decimal.Sub(b.Decimal)}
}
func (a Decimal) Div(b Decimal) Decimal {
return Decimal{a.Decimal.DivRound(b.Decimal, persistentDecimals)}
}
func (a Decimal) Divisible(b Decimal) bool {
if a.Cmp(b) < 0 {
return false
}
div := a.Div(b)
return div.Floor().Persist() == div.Persist()
}
func (a Decimal) Mul(b Decimal) Decimal {
return Decimal{a.Decimal.Mul(b.Decimal).Round(persistentDecimals)}
}
func (a Decimal) Neg() Decimal {
return Decimal{a.Decimal.Neg()}
}
func (a Decimal) Cmp(b Decimal) int {
return a.Decimal.Cmp(b.Decimal)
}
func (a Decimal) Floor() Decimal {
return Decimal{a.Decimal.Floor()}
}
func (a Decimal) Ceil() Decimal {
return Decimal{a.Decimal.Ceil()}
}
func (a Decimal) Round(decimals int32) Decimal {
return Decimal{a.Decimal.Round(decimals)}
}
func (a Decimal) RoundFloor(decimals int32) Decimal {
return a.Mul(NewDecimal(1, -decimals)).Floor().Mul(NewDecimal(1, decimals))
}
func (a Decimal) RoundCeil(decimals int32) Decimal {
return a.Mul(NewDecimal(1, -decimals)).Ceil().Mul(NewDecimal(1, decimals))
}
func (a Decimal) Equal(b Decimal) bool {
return a.Decimal.Equal(b.Decimal)
}
func (a Decimal) Persist() string {
return a.Decimal.String()
}
func (a Decimal) PresentFloor() string {
return a.RoundFloor(presentDecimals).Persist()
}
func (a Decimal) PresentCeil() string {
return a.RoundCeil(presentDecimals).Persist()
}
func (a Decimal) Float64() float64 {
f, _ := a.Decimal.Float64()
return f
}
func (a Decimal) Exhausted() bool {
presentMin := NewDecimal(1, presentDecimals).Decimal.Round(presentDecimals)
return a.RoundFloor(presentDecimals).LessThan(presentMin)
}
func (a Decimal) Source() decimal.Decimal {
return a.Decimal
}