-
Notifications
You must be signed in to change notification settings - Fork 19
/
numeric.go
198 lines (168 loc) · 3.81 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package num
import (
"errors"
"fmt"
"strings"
)
type Numeric struct {
asInt *Int
asUint *Uint
asDecimal *Decimal
}
func (n *Numeric) Clone() *Numeric {
if n.asUint != nil {
nn := &Numeric{}
nn.SetUint(n.Uint().Clone())
return nn
}
if n.asInt != nil {
nn := &Numeric{}
nn.SetInt(n.Int().Clone())
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()
}
if n.asInt != nil {
return n.asInt.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
}
if strings.HasPrefix(s, "-") {
in, _ := IntFromString(s, 10)
return &Numeric{
asInt: in,
}, 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) SetInt(in *Int) *Numeric {
n.asInt = in
n.asDecimal = nil
n.asUint = nil
return n
}
func (n *Numeric) SetUint(u *Uint) *Numeric {
n.asUint = u
n.asDecimal = nil
n.asInt = nil
return n
}
func (n *Numeric) SetDecimal(d *Decimal) *Numeric {
n.asDecimal = d
n.asUint = nil
n.asInt = 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) Int() *Int {
if n.asInt == nil {
return nil
}
in := *n.asInt
return &in
}
func (n *Numeric) IsDecimal() bool {
return n.asDecimal != nil
}
func (n *Numeric) IsUint() bool {
return n.asUint != nil
}
func (n *Numeric) IsInt() bool {
return n.asInt != nil
}