-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatnumber_test.go
144 lines (130 loc) · 7.15 KB
/
formatnumber_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
package accounting
import (
"math/big"
"testing"
"github.com/cockroachdb/apd"
"github.com/shopspring/decimal"
)
func TestFormatNumber(t *testing.T) {
AssertEqual(t, FormatNumber(123456789.213123, 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumber(123456789.213123, 3, ".", ","), "123.456.789,213")
AssertEqual(t, FormatNumber(-12345.123123, 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumber(-1234.123123, 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumber(-123.123123, 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumber(-12.123123, 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumber(-1.123123, 5, ",", "."), "-1.12312")
AssertEqual(t, FormatNumber(-1, 3, ",", "."), "-1.000")
AssertEqual(t, FormatNumber(-10, 3, ",", "."), "-10.000")
AssertEqual(t, FormatNumber(-100, 3, ",", "."), "-100.000")
AssertEqual(t, FormatNumber(-1000, 3, ",", "."), "-1,000.000")
AssertEqual(t, FormatNumber(-10000, 3, ",", "."), "-10,000.000")
AssertEqual(t, FormatNumber(-100000, 3, ",", "."), "-100,000.000")
AssertEqual(t, FormatNumber(-1000000, 3, ",", "."), "-1,000,000.000")
AssertEqual(t, FormatNumber(1, 3, ",", "."), "1.000")
AssertEqual(t, FormatNumber(10, 3, ",", "."), "10.000")
AssertEqual(t, FormatNumber(100, 3, ",", "."), "100.000")
AssertEqual(t, FormatNumber(1000, 3, ",", "."), "1,000.000")
AssertEqual(t, FormatNumber(10000, 3, ",", "."), "10,000.000")
AssertEqual(t, FormatNumber(100000, 3, ",", "."), "100,000.000")
AssertEqual(t, FormatNumber(1000000, 3, ",", "."), "1,000,000.000")
AssertEqual(t, FormatNumber(1000000, 10, " ", "."), "1 000 000.0000000000")
AssertEqual(t, FormatNumber(1000000, 10, " ", "."), "1 000 000.0000000000")
AssertEqual(t, FormatNumber(uint(1000000), 3, ",", "."), "1,000,000.000")
AssertEqual(t, FormatNumber(big.NewRat(77777777, 3), 3, ",", "."), "25,925,925.667")
AssertEqual(t, FormatNumber(big.NewRat(-77777777, 3), 3, ",", "."), "-25,925,925.667")
AssertEqual(t, FormatNumber(big.NewRat(-7777777, 3), 3, ",", "."), "-2,592,592.333")
AssertEqual(t, FormatNumber(big.NewRat(-777776, 3), 3, ",", "."), "-259,258.667")
AssertEqual(t, FormatNumber(apd.New(123456789213123, -6), 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumber(apd.New(-12345123123, -6), 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumber(apd.New(-1234123123, -6), 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumber(apd.New(-123123123, -6), 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumber(apd.New(-12123123, -6), 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumber(apd.New(-1123123, -6), 5, ",", "."), "-1.12312")
d1 := decimal.New(123456789213123, -6)
d2 := decimal.New(-12345123123, -6)
d3 := decimal.New(-1234123123, -6)
d4 := decimal.New(-123123123, -6)
d5 := decimal.New(-12123123, -6)
d6 := decimal.New(-1123123, -6)
AssertEqual(t, FormatNumber(d1, 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumber(d2, 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumber(d3, 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumber(d4, 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumber(d5, 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumber(d6, 5, ",", "."), "-1.12312")
AssertEqual(t, FormatNumber(&d1, 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumber(&d2, 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumber(&d3, 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumber(&d4, 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumber(&d5, 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumber(&d6, 5, ",", "."), "-1.12312")
func() {
defer func() {
recover()
}()
FormatNumber(false, 3, ",", ".") // panic: Unsupported type - bool
}()
func() {
defer func() {
recover()
}()
FormatNumber(big.NewInt(1), 3, ",", ".") // panic: Unsupported type - *big.Int
}()
func() {
type demo struct {
Value int
}
defer func() {
recover()
}()
FormatNumber(demo{Value: 1}, 3, ",", ".") // panic: Unsupported type - *big.Int
}()
}
func TestFormatNumberInt(t *testing.T) {
AssertEqual(t, FormatNumberInt(-1, 3, ",", "."), "-1.000")
AssertEqual(t, FormatNumberInt(-10, 3, ",", "."), "-10.000")
AssertEqual(t, FormatNumberInt(-100, 3, ",", "."), "-100.000")
AssertEqual(t, FormatNumberInt(-1000, 3, ",", "."), "-1,000.000")
AssertEqual(t, FormatNumberInt(-10000, 3, ",", "."), "-10,000.000")
AssertEqual(t, FormatNumberInt(-100000, 3, ",", "."), "-100,000.000")
AssertEqual(t, FormatNumberInt(-1000000, 3, ",", "."), "-1,000,000.000")
AssertEqual(t, FormatNumberInt(1, 3, ",", "."), "1.000")
AssertEqual(t, FormatNumberInt(10, 3, ",", "."), "10.000")
AssertEqual(t, FormatNumberInt(100, 3, ",", "."), "100.000")
AssertEqual(t, FormatNumberInt(1000, 3, ",", "."), "1,000.000")
AssertEqual(t, FormatNumberInt(10000, 3, ",", "."), "10,000.000")
AssertEqual(t, FormatNumberInt(100000, 3, ",", "."), "100,000.000")
AssertEqual(t, FormatNumberInt(1000000, 3, ",", "."), "1,000,000.000")
AssertEqual(t, FormatNumberInt(1000000, 10, " ", "."), "1 000 000.0000000000")
AssertEqual(t, FormatNumberInt(1000000, 10, " ", "."), "1 000 000.0000000000")
}
func TestFormatNumberFloat64(t *testing.T) {
AssertEqual(t, FormatNumberFloat64(123456789.213123, 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumberFloat64(-12345.123123, 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumberFloat64(-1234.123123, 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumberFloat64(-123.123123, 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumberFloat64(-12.123123, 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumberFloat64(-1.123123, 5, ",", "."), "-1.12312")
}
func TestFormatNumberBigRat(t *testing.T) {
AssertEqual(t, FormatNumberBigRat(big.NewRat(77777777, 3), 3, ",", "."), "25,925,925.667")
AssertEqual(t, FormatNumberBigRat(big.NewRat(-77777777, 3), 3, ",", "."), "-25,925,925.667")
AssertEqual(t, FormatNumberBigRat(big.NewRat(-7777777, 3), 3, ",", "."), "-2,592,592.333")
AssertEqual(t, FormatNumberBigRat(big.NewRat(-777776, 3), 3, ",", "."), "-259,258.667")
}
func TestFormatNumberBigDecimal(t *testing.T) {
AssertEqual(t, FormatNumberBigDecimal(apd.New(123456789213123, -6), 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumberBigDecimal(apd.New(-12345123123, -6), 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumberBigDecimal(apd.New(-1234123123, -6), 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumberBigDecimal(apd.New(-123123123, -6), 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumberBigDecimal(apd.New(-12123123, -6), 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumberBigDecimal(apd.New(-1123123, -6), 5, ",", "."), "-1.12312")
}
func TestFormatNumberDecimal(t *testing.T) {
AssertEqual(t, FormatNumberDecimal(decimal.New(123456789213123, -6), 3, ",", "."), "123,456,789.213")
AssertEqual(t, FormatNumberDecimal(decimal.New(-12345123123, -6), 5, ",", "."), "-12,345.12312")
AssertEqual(t, FormatNumberDecimal(decimal.New(-1234123123, -6), 5, ",", "."), "-1,234.12312")
AssertEqual(t, FormatNumberDecimal(decimal.New(-123123123, -6), 5, ",", "."), "-123.12312")
AssertEqual(t, FormatNumberDecimal(decimal.New(-12123123, -6), 5, ",", "."), "-12.12312")
AssertEqual(t, FormatNumberDecimal(decimal.New(-1123123, -6), 5, ",", "."), "-1.12312")
}