/
number_to_human_test.go
106 lines (89 loc) · 2.22 KB
/
number_to_human_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
package gonumbers_test
import (
"fmt"
"testing"
"github.com/hgsigner/gonumbers"
)
func Test_ToHuman(t *testing.T) {
tests := []struct {
in float64
out string
precision int
separator string
addPrecision, addSeparator bool
}{
{in: 123, out: "123"},
{in: 1234, out: "1.23 Thousand"},
{in: 12345, out: "12.3 Thousand"},
{in: 1234567, out: "1.23 Million"},
{in: 1234567890, out: "1.23 Billion"},
{in: 1234567890123, out: "1.23 Trillion"},
{in: 1234567890123456, out: "1.23 Quadrillion"},
{
in: 489939,
addPrecision: true,
precision: 2,
out: "490 Thousand",
},
{
in: 489939,
addPrecision: true,
precision: 4,
out: "489.9 Thousand",
},
{
in: 489939,
addPrecision: true,
precision: 4,
addSeparator: true,
separator: ",",
out: "489,9 Thousand",
},
}
for _, test := range tests {
nth := gonumbers.ToHuman()
if test.addPrecision {
nth.Options(gonumbers.Precision(test.precision))
}
if test.addSeparator {
nth.Options(gonumbers.Separator(test.separator))
}
assert(t, test.out, nth.Perform(test.in))
}
}
func ExampleToHuman() {
nth1 := gonumbers.ToHuman()
fmt.Println(nth1.Perform(123))
nth2 := gonumbers.ToHuman()
fmt.Println(nth2.Perform(1234))
nth3 := gonumbers.ToHuman()
fmt.Println(nth3.Perform(12345))
nth4 := gonumbers.ToHuman()
fmt.Println(nth4.Perform(1234567))
nth5 := gonumbers.ToHuman()
fmt.Println(nth5.Perform(1234567890))
nth6 := gonumbers.ToHuman()
fmt.Println(nth6.Perform(1234567890123))
nth7 := gonumbers.ToHuman()
fmt.Println(nth7.Perform(1234567890123456))
nth8 := gonumbers.ToHuman()
nth8.Options(gonumbers.Precision(2))
fmt.Println(nth8.Perform(489939))
nth9 := gonumbers.ToHuman()
nth9.Options(gonumbers.Precision(4))
fmt.Println(nth9.Perform(489939))
nth10 := gonumbers.ToHuman()
nth10.Options(gonumbers.Precision(4), gonumbers.Separator(","))
fmt.Println(nth10.Perform(489939))
// Output:
// 123
// 1.23 Thousand
// 12.3 Thousand
// 1.23 Million
// 1.23 Billion
// 1.23 Trillion
// 1.23 Quadrillion
// 490 Thousand
// 489.9 Thousand
// 489,9 Thousand
}