forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_example_test.go
116 lines (84 loc) · 2.2 KB
/
utils_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
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
package money_test
import (
"fmt"
"log"
money "github.com/rezaalavi/gmoney"
"github.com/shopspring/decimal"
)
func ExampleSum() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
threePounds := money.New(decimal.NewFromInt(3), "GBP")
sum, err := money.Sum(pound, twoPounds, threePounds)
if err != nil {
log.Fatal(err)
}
fmt.Println(sum.Display())
// Output:
// £6.00
}
func ExampleAverage() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
fourPounds := money.New(decimal.NewFromInt(4), "GBP")
average, err := money.Average(pound, twoPounds, fourPounds)
if err != nil {
log.Fatal(err)
}
fmt.Println(average.Display())
// Output:
// £2.33
}
func ExampleMin() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
fourPounds := money.New(decimal.NewFromInt(4), "GBP")
min, err := money.Min(pound, twoPounds, fourPounds)
if err != nil {
log.Fatal(err)
}
fmt.Println(min.Display())
// Output:
// £1.00
}
func ExampleMax() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
fourPounds := money.New(decimal.NewFromInt(4), "GBP")
max, err := money.Max(pound, twoPounds, fourPounds)
if err != nil {
log.Fatal(err)
}
fmt.Println(max.Display())
// Output:
// £4.00
}
func ExampleSort() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
fourPounds := money.New(decimal.NewFromInt(4), "GBP")
sorted, err := money.Sort([]*money.Money{fourPounds, pound, twoPounds})
if err != nil {
log.Fatal(err)
}
for _, m := range sorted {
fmt.Println(m.Display())
}
// Output:
// £1.00
// £2.00
// £4.00
}
func ExampleMedian() {
pound := money.New(decimal.NewFromInt(1), "GBP")
twoPounds := money.New(decimal.NewFromInt(2), "GBP")
threePounds := money.New(decimal.NewFromInt(3), "GBP")
fourPounds := money.New(decimal.NewFromInt(4), "GBP")
median, err := money.Median(threePounds, pound, twoPounds, fourPounds)
if err != nil {
log.Fatal(err)
}
fmt.Println(median.Display())
// Output:
// £2.50
}