-
Notifications
You must be signed in to change notification settings - Fork 22
/
numeric.go
145 lines (120 loc) · 2.53 KB
/
numeric.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package num
import (
"errors"
"fmt"
"strings"
)
type Numeric struct {
asUint *Uint
asDecimal *Decimal
}
func (n *Numeric) Clone() *Numeric {
if n.asUint != nil {
nn := n
return nn
}
nn := &Numeric{}
if n.asDecimal != nil {
decimal := *n.asDecimal
nn.asDecimal = &decimal
}
return nn
}
func (n *Numeric) String() string {
if n.asUint != nil {
return n.asUint.String()
}
if n.asDecimal != nil {
return n.asDecimal.String()
}
return ""
}
func NumericToString(n *Numeric) string {
if n == nil {
return ""
}
return n.String()
}
func NumericFromString(s string) (*Numeric, error) {
if s == "" {
return nil, nil
}
// Check if the provided string contains a ".", because if it does not,
// the DecimalFromString will return it as int
split := strings.Split(s, ".")
if len(split) > 1 {
d, err := DecimalFromString(s)
if err != nil {
return nil, fmt.Errorf("error obtaining decimal from string: %s", err.Error())
}
return &Numeric{
asDecimal: &d,
}, nil
}
u, _ := UintFromString(s, 10)
return &Numeric{
asUint: u,
}, nil
}
// ScaleTo calculates the current contained value - decimal or uint - scaled to the target decimals.
func (n *Numeric) ScaleTo(op, tdp int64) (*Uint, error) {
base := DecimalFromInt64(10)
if n.asDecimal != nil {
scaled := n.asDecimal.Mul(base.Pow(DecimalFromInt64(tdp)))
r, overflow := UintFromDecimal(scaled)
if overflow {
return nil, errors.New("failed to scale settlement data, overflow occurred")
}
return r, nil
}
if n.asUint != nil {
scaled := base.Pow(DecimalFromInt64(tdp - op))
r, overflow := UintFromDecimal(n.asUint.ToDecimal().Mul(scaled))
if overflow {
return nil, errors.New("failed to scale settlement data, overflow occurred")
}
return r, nil
}
return nil, nil
}
func (n *Numeric) SupportDecimalPlaces(dp int64) bool {
if n.IsDecimal() {
decimalParts := strings.Split(n.Decimal().String(), ".")
if len(decimalParts) > 1 {
if int64(len(decimalParts[1])) > dp {
return false
}
}
}
return true
}
func (n *Numeric) SetUint(u *Uint) *Numeric {
n.asUint = u
n.asDecimal = nil
return n
}
func (n *Numeric) SetDecimal(d *Decimal) *Numeric {
n.asDecimal = d
n.asUint = nil
return n
}
func (n *Numeric) Decimal() *Decimal {
if n.asDecimal == nil {
return nil
}
d := *n.asDecimal
return &d
}
func (n *Numeric) Uint() *Uint {
if n.asUint == nil {
return nil
}
u := *n.asUint
return &u
}
func (n *Numeric) IsDecimal() bool {
return n.asDecimal != nil
}
func (n *Numeric) IsUint() bool {
return n.asUint != nil
}