-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
74 lines (64 loc) · 1.15 KB
/
example_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
package round_test
import (
"encoding/json"
"fmt"
"github.com/portfoliotree/round"
)
type Rates struct {
SomeRate float64 `precision:"2,percent"`
}
type Data struct {
Number float64
Map map[string]float64 `precision:"3"`
Embedded Rates
List []float64
}
func (d Data) String() string {
buf, err := json.MarshalIndent(d, "", "\t")
if err != nil {
panic(err)
}
return string(buf)
}
func Example() {
data := Data{
Number: 1.1111,
Map: map[string]float64{
"4.5555555": 4.5555555,
"4.4444444": 4.4444444,
},
Embedded: Rates{SomeRate: 0.998765},
List: []float64{7, 6.656},
}
fmt.Printf("before: %s\n", data.String())
_ = round.Recursive(&data, 2)
fmt.Printf("after: %s", data.String())
// Output: before: {
// "Number": 1.1111,
// "Map": {
// "4.4444444": 4.4444444,
// "4.5555555": 4.5555555
// },
// "Embedded": {
// "SomeRate": 0.998765
// },
// "List": [
// 7,
// 6.656
// ]
// }
// after: {
// "Number": 1.11,
// "Map": {
// "4.4444444": 4.444,
// "4.5555555": 4.556
// },
// "Embedded": {
// "SomeRate": 99.88
// },
// "List": [
// 7,
// 6.66
// ]
//}
}