-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
386 lines (334 loc) · 7.24 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package decimal
import (
"errors"
"fmt"
"github.com/ericlagergren/decimal"
)
var zero Decimal = Zero()
type Decimal struct {
nat *decimal.Big
}
func (d *Decimal) native() *decimal.Big {
if d.nat == nil {
d.nat = decimal.New(0, 0)
}
return d.nat
}
func Zero() Decimal {
return New(0, 0)
}
func New(value int64, scale int32) Decimal {
return Decimal{
decimal.New(value, int(scale)),
}
}
func NewFromInt(i int) Decimal {
return NewFromInt64(int64(i))
}
func NewFromInt8(i int8) Decimal {
return NewFromInt64(int64(i))
}
func NewFromInt16(i int16) Decimal {
return NewFromInt64(int64(i))
}
func NewFromInt32(i int32) Decimal {
return NewFromInt64(int64(i))
}
func NewFromInt64(i int64) Decimal {
return Decimal{
decimal.New(i, 0),
}
}
func NewFromUint(i uint) Decimal {
return NewFromUint64(uint64(i))
}
func NewFromUint8(i uint8) Decimal {
return NewFromUint64(uint64(i))
}
func NewFromUint16(i uint16) Decimal {
return NewFromUint64(uint64(i))
}
func NewFromUint32(i uint32) Decimal {
return NewFromUint64(uint64(i))
}
func NewFromUint64(i uint64) Decimal {
d := decimal.New(0, 0)
d.SetUint64(i)
return Decimal{d}
}
func NewFromFloat32(f float32) Decimal {
return NewFromFloat64(float64(f))
}
func NewFromFloat64(f float64) Decimal {
d := decimal.New(0, 0)
d.SetFloat64(f)
return Decimal{d}
}
func NewFromString(s string) (Decimal, error) {
d := decimal.New(0, 0)
_, ok := d.SetString(s)
if !ok {
return Decimal{}, errors.New("Invalid decimal")
}
if d.IsNaN(0) {
return Decimal{}, errors.New("Invalid decimal")
}
return Decimal{d}, nil
}
func MustNewFromString(s string) Decimal {
d, err := NewFromString(s)
if err != nil {
panic(err)
}
return d
}
func NewFromDecimal(d Decimal) Decimal {
if d.nat == nil {
return Zero()
}
cpy := decimal.New(0, 0)
cpy.Copy(d.nat)
return Decimal{cpy}
}
func NewFromInterface(value interface{}) (Decimal, error) {
switch v := value.(type) {
case float32:
return NewFromFloat32(v), nil
case float64:
return NewFromFloat64(v), nil
case int:
return NewFromInt(v), nil
case int8:
return NewFromInt8(v), nil
case int16:
return NewFromInt16(v), nil
case int32:
return NewFromInt32(v), nil
case int64:
return NewFromInt64(v), nil
case uint:
return NewFromUint(v), nil
case uint8:
return NewFromUint8(v), nil
case uint16:
return NewFromUint16(v), nil
case uint32:
return NewFromUint32(v), nil
case uint64:
return NewFromUint64(v), nil
case string:
return NewFromString(v)
case []byte:
return NewFromString(string(v))
case Decimal:
return NewFromDecimal(v), nil
case *Decimal:
return NewFromDecimal(*v), nil
default:
if buf, ok := v.([]byte); ok {
return NewFromString(string(buf))
}
tmp, err := NewFromString(fmt.Sprintf("%v", v))
if err != nil {
return Decimal{}, fmt.Errorf("Unable to create decimal from value type %T: %v", v, err)
}
return Decimal{tmp.native()}, nil
}
}
func MustNewFromInterface(value interface{}) Decimal {
d, err := NewFromInterface(value)
if err != nil {
panic(err)
}
return d
}
func (d Decimal) Int8() (int8, error) {
i, ok := d.native().Int64()
if !ok {
return 0, fmt.Errorf("`%s' not an int8", d.String())
}
return int8(i), nil
}
func (d Decimal) MustInt8() int8 {
v, err := d.Int8()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Int16() (int16, error) {
i, ok := d.native().Int64()
if !ok {
return 0, fmt.Errorf("`%s' not an int16", d.String())
}
return int16(i), nil
}
func (d Decimal) MustInt16() int16 {
v, err := d.Int16()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Int32() (int32, error) {
i, ok := d.native().Int64()
if !ok {
return 0, fmt.Errorf("`%s' not an int32", d.String())
}
return int32(i), nil
}
func (d Decimal) MustInt32() int32 {
v, err := d.Int32()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Int64() (int64, error) {
i, ok := d.native().Int64()
if !ok {
return 0, fmt.Errorf("`%s' not an int64", d.String())
}
return i, nil
}
func (d Decimal) MustInt64() int64 {
v, err := d.Int64()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Uint8() (uint8, error) {
i, ok := d.native().Uint64()
if !ok {
return 0, fmt.Errorf("`%s' not an uint8", d.String())
}
return uint8(i), nil
}
func (d Decimal) MustUint8() uint8 {
v, err := d.Uint8()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Uint16() (uint16, error) {
i, ok := d.native().Uint64()
if !ok {
return 0, fmt.Errorf("`%s' not an uint16", d.String())
}
return uint16(i), nil
}
func (d Decimal) MustUint16() uint16 {
v, err := d.Uint16()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Uint32() (uint32, error) {
i, ok := d.native().Uint64()
if !ok {
return 0, fmt.Errorf("`%s' not an uint32", d.String())
}
return uint32(i), nil
}
func (d Decimal) MustUint32() uint32 {
v, err := d.Uint32()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Uint64() (uint64, error) {
i, ok := d.native().Uint64()
if !ok {
return 0, fmt.Errorf("`%s' not an uint64", d.String())
}
return i, nil
}
func (d Decimal) MustUint64() uint64 {
v, err := d.Uint64()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Int() (int, error) {
i, ok := d.native().Int64()
if !ok {
return 0, fmt.Errorf("`%s' not an int", d.String())
}
return int(i), nil
}
func (d Decimal) MustInt() int {
v, err := d.Int()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Uint() (uint, error) {
i, ok := d.native().Uint64()
if !ok {
return 0, fmt.Errorf("`%s' not an uint", d.String())
}
return uint(i), nil
}
func (d Decimal) MustUint() uint {
v, err := d.Uint()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Float32() (float32, error) {
i, ok := d.native().Float64()
if !ok {
return 0, fmt.Errorf("`%s' not an float32", d.String())
}
return float32(i), nil
}
func (d Decimal) MustFloat32() float32 {
v, err := d.Float32()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) Float64() (float64, error) {
i, ok := d.native().Float64()
if !ok {
return 0, fmt.Errorf("`%s' not an float64", d.String())
}
return i, nil
}
func (d Decimal) MustFloat64() float64 {
v, err := d.Float64()
if err != nil {
panic(err)
}
return v
}
func (d Decimal) String() string {
if d.native() == nil || d.Equals(zero) {
return "0"
}
return fmt.Sprintf("%f", d)
}
func (d Decimal) Bytes() []byte {
return []byte(d.String())
}
// Format implements the fmt.Formatter interface.
func (d Decimal) Format(s fmt.State, c rune) {
d.native().Format(s, c)
}
// IsNaN is a method that wraps IsNaN method of the Big type. Big type method expects an integer argument called quiet. Here's a breakdown of the input values and what they mean:
// - quiet > 0: The function will return true if the Big value x is a quiet NaN.
// - quiet < 0: The function will return true if the Big value x is a signaling NaN.
// - quiet == 0: The function will return true if the Big value x is either a quiet or signaling NaN.
// We are passing 0 as the argument to the IsNaN method, which means that we are checking if the Big value x is either a quiet or signaling NaN.
// For more information, see https://stackoverflow.com/questions/18118408/what-is-the-difference-between-quiet-nan-and-signaling-nan
func (d Decimal) IsNaN() bool {
return d.native().IsNaN(0)
}