-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecimal_test.go
More file actions
69 lines (55 loc) · 2.26 KB
/
Copy pathdecimal_test.go
File metadata and controls
69 lines (55 loc) · 2.26 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
package number
import (
"math/big"
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
func TestDecimal(t *testing.T) {
assert := assert.New(t)
assert.Equal("0", Zero().Persist())
assert.True(FromString("0.00000000000000100000000000000003").Equal(FromString("0.000000000000001000000000000000031")))
assert.False(FromString("0.00000000000000100000000000000002").Equal(FromString("0.000000000000001000000000000000031")))
a := FromString("1.0000000000000000300000")
assert.Equal("1.00000000000000003", a.Persist())
b := FromString("1.0000000000000000300000")
assert.Equal("1.00000000000000003", b.Persist())
assert.Equal("1", b.PresentFloor())
assert.Equal("1.00000001", b.PresentCeil())
c := NewDecimal(1e8, 7)
assert.Equal("10", c.Persist())
assert.Equal("10", c.PresentFloor())
assert.Equal("10", c.PresentCeil())
d := NewDecimal(1, 15)
assert.Equal("0.000000000000001", d.Persist())
assert.Equal(1, d.Cmp(Zero()))
assert.True(d.Exhausted())
assert.Equal("0.00000000000000100000000000000003", a.Mul(d).Persist())
assert.Equal("0", a.Mul(d).PresentFloor())
assert.Equal("0.00000001", a.Mul(d).PresentCeil())
assert.True(a.Mul(d).Exhausted())
assert.Equal("1000000000000000.03", a.Div(d).Persist())
assert.False(a.Div(d).Exhausted())
e, _ := new(big.Int).SetString("0x32748932FA632BFF8323282", 0)
assert.Equal("975945861920638759748121218", e.Text(10))
f := FromString(e.Text(10)).Mul(NewDecimal(1, 18))
assert.Equal("975945861.920638759748121218", f.Persist())
assert.Equal("975945861.92063875", f.PresentFloor())
assert.Equal("975945861.92063876", f.PresentCeil())
assert.False(f.Exhausted())
g := NewDecimal(1e10, 0)
assert.Equal("10000000000", g.Persist())
h := FromString(e.Text(10)).Div(FromString("1000000000000000000"))
assert.Equal("975945861.920638759748121218", h.Persist())
assert.Equal("975945861.92063875", h.PresentFloor())
assert.Equal("975945861.92063876", h.PresentCeil())
assert.True(f.Equal(h))
i := FromString("465.505437")
assert.Equal("465.50543", i.RoundFloor(5).Persist())
assert.Equal("465.505437", i.RoundFloor(8).Persist())
j := FromString("0.00000000999")
assert.Equal("0", j.RoundFloor(8).Persist())
assert.True(j.Exhausted())
source := j.Source()
assert.IsType(new(decimal.Decimal), &source)
}