Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
309 lines (264 loc) · 7.1 KB
/
Copy pathexample_test.go
File metadata and controls
309 lines (264 loc) · 7.1 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package currency_test
import (
"fmt"
"strconv"
"github.com/bojanz/currency"
)
func ExampleNewAmount() {
amount, _ := currency.NewAmount("24.49", "USD")
fmt.Println(amount)
fmt.Println(amount.Number())
fmt.Println(amount.CurrencyCode())
// Output: 24.49 USD
// 24.49
// USD
}
func ExampleNewAmountFromInt64() {
firstAmount, _ := currency.NewAmountFromInt64(2449, "USD")
secondAmount, _ := currency.NewAmountFromInt64(5000, "USD")
thirdAmount, _ := currency.NewAmountFromInt64(60, "JPY")
fmt.Println(firstAmount)
fmt.Println(secondAmount)
fmt.Println(thirdAmount)
// Output: 24.49 USD
// 50.00 USD
// 60 JPY
}
func ExampleAmount_Int64() {
firstAmount, _ := currency.NewAmount("24.49", "USD")
secondAmount, _ := currency.NewAmount("50", "USD")
thirdAmount, _ := currency.NewAmount("60", "JPY")
firstInt, _ := firstAmount.Int64()
secondInt, _ := secondAmount.Int64()
thirdInt, _ := thirdAmount.Int64()
fmt.Println(firstInt, secondInt, thirdInt)
// Output: 2449 5000 60
}
func ExampleAmount_Convert() {
amount, _ := currency.NewAmount("20.99", "USD")
amount, _ = amount.Convert("EUR", "0.91")
fmt.Println(amount)
fmt.Println(amount.Round())
// Output: 19.1009 EUR
// 19.10 EUR
}
func ExampleAmount_Add() {
firstAmount, _ := currency.NewAmount("20.99", "USD")
secondAmount, _ := currency.NewAmount("3.50", "USD")
totalAmount, _ := firstAmount.Add(secondAmount)
fmt.Println(totalAmount)
// Output: 24.49 USD
}
func ExampleAmount_Add_sum() {
// Any currency.Amount can be added to the zero value.
var sum currency.Amount
for i := 0; i <= 4; i++ {
a, _ := currency.NewAmount(strconv.Itoa(i), "AUD")
sum, _ = sum.Add(a)
}
fmt.Println(sum) // 0 + 1 + 2 + 3 + 4 = 10
// Output: 10 AUD
}
func ExampleAmount_Sub() {
baseAmount, _ := currency.NewAmount("20.99", "USD")
discountAmount, _ := currency.NewAmount("5.00", "USD")
amount, _ := baseAmount.Sub(discountAmount)
fmt.Println(amount)
// Output: 15.99 USD
}
func ExampleAmount_Sub_diff() {
// Any currency.Amount can be subtracted from the zero value.
var diff currency.Amount
for i := 0; i <= 4; i++ {
a, _ := currency.NewAmount(strconv.Itoa(i), "AUD")
diff, _ = diff.Sub(a)
}
fmt.Println(diff) // 0 - 1 - 2 - 3 - 4 = -10
// Output: -10 AUD
}
func ExampleAmount_Mul() {
amount, _ := currency.NewAmount("20.99", "USD")
taxAmount, _ := amount.Mul("0.20")
fmt.Println(taxAmount)
fmt.Println(taxAmount.Round())
// Output: 4.1980 USD
// 4.20 USD
}
func ExampleAmount_Div() {
totalAmount, _ := currency.NewAmount("99.99", "USD")
amount, _ := totalAmount.Div("3")
fmt.Println(amount)
// Output: 33.33 USD
}
func ExampleAmount_Split() {
amount, _ := currency.NewAmount("10.00", "USD")
parts, _ := amount.Split(3)
for _, part := range parts {
fmt.Println(part)
}
// Output: 3.34 USD
// 3.33 USD
// 3.33 USD
}
func ExampleAmount_Allocate() {
amount, _ := currency.NewAmount("100.00", "USD")
parts, _ := amount.Allocate(1, 2, 3)
for _, part := range parts {
fmt.Println(part)
}
// Output: 16.67 USD
// 33.33 USD
// 50.00 USD
}
func ExampleAmount_Round() {
firstAmount, _ := currency.NewAmount("12.345", "USD")
secondAmount, _ := currency.NewAmount("12.345", "JPY")
fmt.Println(firstAmount.Round())
fmt.Println(secondAmount.Round())
// Output: 12.35 USD
// 12 JPY
}
func ExampleAmount_RoundTo() {
amount, _ := currency.NewAmount("12.345", "USD")
for _, digits := range []uint8{4, 3, 2, 1, 0} {
fmt.Println(amount.RoundTo(digits, currency.RoundHalfUp))
}
// Output: 12.3450 USD
// 12.345 USD
// 12.35 USD
// 12.3 USD
// 12 USD
}
func ExampleNewLocale() {
firstLocale := currency.NewLocale("en-US")
fmt.Println(firstLocale)
fmt.Println(firstLocale.Language, firstLocale.Territory)
// Locale IDs are normalized.
secondLocale := currency.NewLocale("sr_rs_latn")
fmt.Println(secondLocale)
fmt.Println(secondLocale.Language, secondLocale.Script, secondLocale.Territory)
// Output: en-US
// en US
// sr-Latn-RS
// sr Latn RS
}
func ExampleLocale_GetParent() {
locale := currency.NewLocale("sr-Cyrl-RS")
for {
fmt.Println(locale)
locale = locale.GetParent()
if locale.IsEmpty() {
break
}
}
// Output: sr-Cyrl-RS
// sr-Cyrl
// sr
// en
}
func ExampleFormatter_Format() {
locale := currency.NewLocale("tr")
formatter := currency.NewFormatter(locale)
amount, _ := currency.NewAmount("1245.988", "EUR")
fmt.Println(formatter.Format(amount))
formatter.MaxDigits = 2
fmt.Println(formatter.Format(amount))
formatter.NoGrouping = true
amount, _ = currency.NewAmount("1245", "EUR")
fmt.Println(formatter.Format(amount))
formatter.MinDigits = 0
fmt.Println(formatter.Format(amount))
formatter.CurrencyDisplay = currency.DisplayNone
fmt.Println(formatter.Format(amount))
// Output: €1.245,988
// €1.245,99
// €1245,00
// €1245
// 1245
}
func ExampleFormatter_Parse() {
locale := currency.NewLocale("tr")
formatter := currency.NewFormatter(locale)
amount, _ := formatter.Parse("€1.234,59", "EUR")
fmt.Println(amount)
amount, _ = formatter.Parse("EUR 1.234,59", "EUR")
fmt.Println(amount)
amount, _ = formatter.Parse("1.234,59", "EUR")
fmt.Println(amount)
// Output: 1234.59 EUR
// 1234.59 EUR
// 1234.59 EUR
}
func ExampleForCountryCode() {
currencyCode, ok := currency.ForCountryCode("US")
fmt.Println(currencyCode, ok)
currencyCode, ok = currency.ForCountryCode("FR")
fmt.Println(currencyCode, ok)
// Non-existent country code.
_, ok = currency.ForCountryCode("XX")
fmt.Println(ok)
// Output: USD true
// EUR true
// false
}
func ExampleGetNumericCode() {
numericCode, ok := currency.GetNumericCode("USD")
fmt.Println(numericCode, ok)
// Non-existent currency code.
numericCode, ok = currency.GetNumericCode("XXX")
fmt.Println(numericCode, ok)
// Output: 840 true
// 000 false
}
func ExampleGetDigits() {
digits, ok := currency.GetDigits("USD")
fmt.Println(digits, ok)
// Non-existent currency code.
digits, ok = currency.GetDigits("XXX")
fmt.Println(digits, ok)
// Output: 2 true
// 0 false
}
func ExampleGetSymbol() {
locale := currency.NewLocale("en")
symbol, ok := currency.GetSymbol("USD", locale)
fmt.Println(symbol, ok)
// Non-existent currency code.
symbol, ok = currency.GetSymbol("XXX", locale)
fmt.Println(symbol, ok)
// Output: $ true
// XXX false
}
func ExampleRegister() {
locale := currency.NewLocale("fr")
// Add a custom currency.
currency.Register("BTC", currency.Definition{
NumericCode: "1000",
Digits: 8,
DefaultSymbol: "₿",
})
valid := currency.IsValid("BTC")
fmt.Println(valid)
// Override an existing currency, keeping the built-in symbols.
currency.Register("AUD", currency.Definition{
NumericCode: "036",
Digits: 3,
})
digits, _ := currency.GetDigits("AUD")
symbol, _ := currency.GetSymbol("AUD", locale)
fmt.Println(digits, symbol)
// Override an existing currency and its symbols.
currency.Register("AUD", currency.Definition{
NumericCode: "036",
Digits: 3,
DefaultSymbol: "$$",
})
digits, _ = currency.GetDigits("AUD")
symbol, _ = currency.GetSymbol("AUD", locale)
fmt.Println(digits, symbol)
// Output: true
// 3 $AU
// 3 $$
}