-
Notifications
You must be signed in to change notification settings - Fork 5
/
doc_test.go
528 lines (465 loc) · 11.5 KB
/
doc_test.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package udecimal
import (
"fmt"
)
func ExampleSetDefaultPrecision() {
SetDefaultPrecision(10)
defer SetDefaultPrecision(19)
a := MustParse("1.23")
b := MustParse("4.12475")
c, _ := a.Div(b)
fmt.Println(c)
// Output:
// 0.2981998909
}
func ExampleMustFromFloat64() {
fmt.Println(MustFromFloat64(1.234))
// cautious: result will lose some precision when converting to decimal
fmt.Println(MustFromFloat64(123456789.1234567890123456789))
// Output:
// 1.234
// 123456789.12345679
}
func ExampleNewFromFloat64() {
fmt.Println(NewFromFloat64(1.234))
// cautious: result will lose some precision when converting to decimal
fmt.Println(NewFromFloat64(123456789.1234567890123456789))
// Output:
// 1.234 <nil>
// 123456789.12345679 <nil>
}
func ExampleMustFromInt64() {
fmt.Println(MustFromInt64(123, 3))
fmt.Println(MustFromInt64(-12345, 2))
// Output:
// 0.123
// -123.45
}
func ExampleNewFromInt64() {
fmt.Println(NewFromInt64(123, 3))
fmt.Println(NewFromInt64(12345, 20))
// Output:
// 0.123 <nil>
// 0 precision out of range. Only support maximum 19 digits after the decimal point
}
func ExampleMustFromUint64() {
fmt.Println(MustFromUint64(123, 3))
fmt.Println(MustFromUint64(12345, 2))
// Output:
// 0.123
// 123.45
}
func ExampleNewFromUint64() {
fmt.Println(NewFromUint64(123, 3))
fmt.Println(NewFromUint64(12345, 2))
fmt.Println(NewFromUint64(12345, 20))
// Output:
// 0.123 <nil>
// 123.45 <nil>
// 0 precision out of range. Only support maximum 19 digits after the decimal point
}
func ExampleMustParse() {
fmt.Println(MustParse("1234567890123456789.1234567890123456789"))
fmt.Println(MustParse("-1234567890123456789.1234567890123456789"))
fmt.Println(MustParse("-0.00007890123456789"))
// Output:
// 1234567890123456789.1234567890123456789
// -1234567890123456789.1234567890123456789
// -0.00007890123456789
}
func ExampleParse() {
fmt.Println(Parse("1234567890123456789.1234567890123456789"))
fmt.Println(Parse("-1234567890123456789.1234567890123456789"))
fmt.Println(Parse("-0.00007890123456789"))
// error cases
fmt.Println(Parse("0.12345678901234567890123"))
fmt.Println(Parse(""))
fmt.Println(Parse("1.123.123"))
// Output:
// 1234567890123456789.1234567890123456789 <nil>
// -1234567890123456789.1234567890123456789 <nil>
// -0.00007890123456789 <nil>
// 0 precision out of range. Only support maximum 19 digits after the decimal point
// 0 parse empty string
// 0 invalid format: can't parse '1.123.123' to Decimal
}
func ExampleNewFromHiLo() {
fmt.Println(NewFromHiLo(false, 1, 1, 10))
fmt.Println(NewFromHiLo(true, 0, 123456, 4))
// Output:
// 1844674407.3709551617 <nil>
// -12.3456 <nil>
}
func ExampleDecimal_Abs() {
fmt.Println(MustParse("-123.45").Abs())
// Output:
// 123.45
}
func ExampleDecimal_Add() {
a := MustParse("1.23")
b := MustParse("4.12475")
c := a.Add(b)
fmt.Println(c)
// Output:
// 5.35475
}
func ExampleDecimal_Add64() {
a := MustParse("1.23")
c := a.Add64(4)
fmt.Println(c)
// Output:
// 5.23
}
func ExampleDecimal_Ceil() {
fmt.Println(MustParse("1.23").Ceil())
// Output:
// 2
}
func ExampleDecimal_Cmp() {
fmt.Println(MustParse("1.23").Cmp(MustParse("4.12475")))
fmt.Println(MustParse("1.23").Cmp(MustParse("1.23")))
fmt.Println(MustParse("1.23").Cmp(MustParse("0.12475")))
// Output:
// -1
// 0
// 1
}
func ExampleDecimal_Equal() {
fmt.Println(MustParse("1.123").Equal(MustParse("-1.123")))
fmt.Println(MustParse("1.123").Equal(MustParse("1.1230000")))
// Output:
// false
// true
}
func ExampleDecimal_Div() {
fmt.Println(MustParse("1.23").Div(MustParse("4.12475")))
fmt.Println(MustParse("1.23").Div(MustParse("0")))
// Output:
// 0.2981998909024789381 <nil>
// 0 can't divide by zero
}
func ExampleDecimal_Div64() {
fmt.Println(MustParse("1.23").Div64(4))
fmt.Println(MustParse("1.23").Div64(0))
// Output:
// 0.3075 <nil>
// 0 can't divide by zero
}
func ExampleDecimal_QuoRem() {
fmt.Println(MustParse("1.23").QuoRem(MustParse("0.5")))
fmt.Println(MustParse("1.23").QuoRem(MustParse("0")))
// Output:
// 2 0.23 <nil>
// 0 0 can't divide by zero
}
func ExampleDecimal_Mod() {
fmt.Println(MustParse("1.23").Mod(MustParse("0.5")))
fmt.Println(MustParse("1.23").Mod(MustParse("0")))
// Output:
// 0.23 <nil>
// 0 can't divide by zero
}
func ExampleDecimal_Sub() {
a := MustParse("1.23")
b := MustParse("4.12475")
c := a.Sub(b)
fmt.Println(c)
// Output:
// -2.89475
}
func ExampleDecimal_Sub64() {
a := MustParse("1.23")
c := a.Sub64(4)
fmt.Println(c)
// Output:
// -2.77
}
func ExampleDecimal_Mul() {
a := MustParse("1.23")
b := MustParse("4.12475")
c := a.Mul(b)
fmt.Println(c)
// Output:
// 5.0734425
}
func ExampleDecimal_Mul64() {
a := MustParse("1.23")
c := a.Mul64(4)
fmt.Println(c)
// Output:
// 4.92
}
func ExampleDecimal_Floor() {
fmt.Println(MustParse("1.23").Floor())
fmt.Println(MustParse("-1.23").Floor())
// Output:
// 1
// -2
}
func ExampleDecimal_Int64() {
fmt.Println(MustParse("1.23").Int64())
fmt.Println(MustParse("1234567890123456789.1234567890123456789").Int64())
fmt.Println(MustParse("-1234567890123456789.1234567890123456789").Int64())
fmt.Println(MustParse("0.123").Int64())
fmt.Println(MustParse("-0.123").Int64())
fmt.Println(MustParse("12345678901234567890123456.123456").Int64())
// Output:
// 1 <nil>
// 1234567890123456789 <nil>
// -1234567890123456789 <nil>
// 0 <nil>
// 0 <nil>
// 0 integer part is too large to fit in int64
}
func ExampleDecimal_InexactFloat64() {
fmt.Println(MustParse("1.23").InexactFloat64())
fmt.Println(MustParse("123456789.123456789").InexactFloat64())
// Output:
// 1.23
// 1.2345678912345679e+08
}
func ExampleDecimal_IsNeg() {
fmt.Println(MustParse("1.23").IsNeg())
fmt.Println(MustParse("-1.23").IsNeg())
fmt.Println(MustParse("0").IsNeg())
// Output:
// false
// true
// false
}
func ExampleDecimal_IsPos() {
fmt.Println(MustParse("1.23").IsPos())
fmt.Println(MustParse("-1.23").IsPos())
fmt.Println(MustParse("0").IsPos())
// Output:
// true
// false
// false
}
func ExampleDecimal_IsZero() {
fmt.Println(MustParse("1.23").IsZero())
fmt.Println(MustParse("0").IsZero())
// Output:
// false
// true
}
func ExampleDecimal_MarshalBinary() {
fmt.Println(MustParse("1.23").MarshalBinary())
fmt.Println(MustParse("-1.2345").MarshalBinary())
fmt.Println(MustParse("1234567890123456789.1234567890123456789").MarshalBinary())
// Output:
// [0 2 11 0 0 0 0 0 0 0 123] <nil>
// [1 4 11 0 0 0 0 0 0 48 57] <nil>
// [0 19 19 9 73 176 246 240 2 51 19 211 181 5 249 181 241 129 21] <nil>
}
func ExampleDecimal_MarshalJSON() {
a, _ := MustParse("1.23").MarshalJSON()
b, _ := MustParse("-1.2345").MarshalJSON()
c, _ := MustParse("1234567890123456789.1234567890123456789").MarshalJSON()
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
// Output:
// "1.23"
// "-1.2345"
// "1234567890123456789.1234567890123456789"
}
func ExampleDecimal_MarshalText() {
a, _ := MustParse("1.23").MarshalText()
b, _ := MustParse("-1.2345").MarshalText()
c, _ := MustParse("1234567890123456789.1234567890123456789").MarshalText()
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(c))
// Output:
// 1.23
// -1.2345
// 1234567890123456789.1234567890123456789
}
func ExampleDecimal_Neg() {
fmt.Println(MustParse("1.23").Neg())
fmt.Println(MustParse("-1.23").Neg())
// Output:
// -1.23
// 1.23
}
func ExampleDecimal_PowInt() {
fmt.Println(MustParse("1.23").PowInt(2))
fmt.Println(MustParse("1.23").PowInt(0))
fmt.Println(MustParse("1.23").PowInt(-2))
// Output:
// 1.5129
// 1
// 0.6609822195782933439
}
func ExampleDecimal_PowToIntPart() {
fmt.Println(MustParse("1.23").PowToIntPart(MustParse("2.123")))
fmt.Println(MustParse("1.23").PowToIntPart(MustParse("0.123")))
fmt.Println(MustParse("1.23").PowToIntPart(MustParse("-2.123")))
fmt.Println(MustParse("0").PowToIntPart(MustParse("0.123")))
fmt.Println(MustParse("0").PowToIntPart(MustParse("-2.123")))
// Output:
// 1.5129 <nil>
// 1 <nil>
// 0.6609822195782933439 <nil>
// 1 <nil>
// 0 can't raise zero to a negative power
}
func ExampleDecimal_PowInt32() {
fmt.Println(MustParse("1.23").PowInt32(2))
fmt.Println(MustParse("1.23").PowInt32(0))
fmt.Println(MustParse("1.23").PowInt32(-2))
fmt.Println(MustParse("0").PowInt32(0))
fmt.Println(MustParse("0").PowInt32(-2))
// Output:
// 1.5129 <nil>
// 1 <nil>
// 0.6609822195782933439 <nil>
// 1 <nil>
// 0 can't raise zero to a negative power
}
func ExampleDecimal_Prec() {
fmt.Println(MustParse("1.23").Prec())
// Output:
// 2
}
func ExampleDecimal_PrecUint() {
fmt.Println(MustParse("1.23456").PrecUint())
// Output:
// 5
}
func ExampleDecimal_RoundBank() {
fmt.Println(MustParse("1.12345").RoundBank(4))
fmt.Println(MustParse("1.12335").RoundBank(4))
fmt.Println(MustParse("1.5").RoundBank(0))
fmt.Println(MustParse("-1.5").RoundBank(0))
// Output:
// 1.1234
// 1.1234
// 2
// -2
}
func ExampleDecimal_RoundAwayFromZero() {
fmt.Println(MustParse("1.12").RoundAwayFromZero(1))
fmt.Println(MustParse("1.15").RoundAwayFromZero(1))
fmt.Println(MustParse("-1.12").RoundAwayFromZero(1))
fmt.Println(MustParse("-1.15").RoundAwayFromZero(1))
// Output:
// 1.2
// 1.2
// -1.2
// -1.2
}
func ExampleDecimal_RoundHAZ() {
fmt.Println(MustParse("1.12345").RoundHAZ(4))
fmt.Println(MustParse("1.12335").RoundHAZ(4))
fmt.Println(MustParse("1.5").RoundHAZ(0))
fmt.Println(MustParse("-1.5").RoundHAZ(0))
// Output:
// 1.1235
// 1.1234
// 2
// -2
}
func ExampleDecimal_RoundHTZ() {
fmt.Println(MustParse("1.12345").RoundHTZ(4))
fmt.Println(MustParse("1.12335").RoundHTZ(4))
fmt.Println(MustParse("1.5").RoundHTZ(0))
fmt.Println(MustParse("-1.5").RoundHTZ(0))
// Output:
// 1.1234
// 1.1233
// 1
// -1
}
func ExampleDecimal_Scan() {
var a Decimal
_ = a.Scan("1.23")
fmt.Println(a)
// Output:
// 1.23
}
func ExampleDecimal_Sign() {
fmt.Println(MustParse("1.23").Sign())
fmt.Println(MustParse("-1.23").Sign())
fmt.Println(MustParse("0").Sign())
// Output:
// 1
// -1
// 0
}
func ExampleDecimal_Sqrt() {
fmt.Println(MustParse("1.21").Sqrt())
fmt.Println(MustParse("0").Sqrt())
fmt.Println(MustParse("-1.21").Sqrt())
// Output:
// 1.1 <nil>
// 0 <nil>
// 0 can't calculate square root of negative number
}
func ExampleDecimal_String() {
fmt.Println(MustParse("1.23").String())
fmt.Println(MustParse("-1.230000").String())
// Output:
// 1.23
// -1.23
}
func ExampleDecimal_StringFixed() {
fmt.Println(MustParse("1").StringFixed(2))
fmt.Println(MustParse("1.23").StringFixed(4))
fmt.Println(MustParse("-1.230000").StringFixed(5))
// Output:
// 1.00
// 1.2300
// -1.23000
}
func ExampleDecimal_Trunc() {
fmt.Println(MustParse("1.23").Trunc(1))
fmt.Println(MustParse("-1.23").Trunc(5))
// Output:
// 1.2
// -1.23
}
func ExampleDecimal_UnmarshalBinary() {
var a Decimal
_ = a.UnmarshalBinary([]byte{0, 2, 11, 0, 0, 0, 0, 0, 0, 0, 123})
fmt.Println(a)
// Output:
// 1.23
}
func ExampleDecimal_UnmarshalJSON() {
var a Decimal
_ = a.UnmarshalJSON([]byte("1.23"))
fmt.Println(a)
// Output:
// 1.23
}
func ExampleDecimal_UnmarshalText() {
var a Decimal
_ = a.UnmarshalText([]byte("1.23"))
fmt.Println(a)
// Output:
// 1.23
}
func ExampleDecimal_Value() {
fmt.Println(MustParse("1.2345").Value())
// Output:
// 1.2345 <nil>
}
func ExampleNullDecimal_Scan() {
var a, b NullDecimal
_ = a.Scan("1.23")
_ = b.Scan(nil)
fmt.Println(a)
fmt.Println(b)
// Output:
// {1.23 true}
// {0 false}
}
func ExampleNullDecimal_Value() {
fmt.Println(NullDecimal{Decimal: MustParse("1.2345"), Valid: true}.Value())
fmt.Println(NullDecimal{}.Value())
// Output:
// 1.2345 <nil>
// <nil> <nil>
}