-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdoc_test.go
2182 lines (1989 loc) · 53.3 KB
/
doc_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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package money_test
import (
"encoding/json"
"encoding/xml"
"fmt"
"strconv"
"strings"
"github.com/govalues/decimal"
"github.com/govalues/money"
)
func TaxAmount(price money.Amount, taxRate decimal.Decimal) (money.Amount, money.Amount, error) {
// Subtotal
one := taxRate.One()
taxRate, err := taxRate.Add(one)
if err != nil {
return money.Amount{}, money.Amount{}, err
}
subtotal, err := price.Quo(taxRate)
if err != nil {
return money.Amount{}, money.Amount{}, err
}
// Function depends on the locax tax laws
subtotal = subtotal.TruncToCurr()
// Tax Amount
tax, err := price.Sub(subtotal)
if err != nil {
return money.Amount{}, money.Amount{}, err
}
return subtotal, tax, nil
}
// In this example, the sales tax amount is calculated for a product with
// a given price after tax, using a specified tax rate.
func Example_taxCalculation() {
price := money.MustParseAmount("USD", "9.99")
taxRate := decimal.MustParse("0.0725")
subtotal, tax, err := TaxAmount(price, taxRate)
if err != nil {
panic(err)
}
fmt.Printf("Subtotal = %v\n", subtotal)
fmt.Printf("Sales tax %-6k = %v\n", taxRate, tax)
fmt.Printf("Total price = %v\n", price)
// Output:
// Subtotal = USD 9.31
// Sales tax 7.25% = USD 0.68
// Total price = USD 9.99
}
type StatementLine struct {
Month int
Days int
Interest money.Amount
Balance money.Amount
}
type Statement []StatementLine
func (s Statement) Append(month, days int, interest, balance money.Amount) Statement {
line := StatementLine{
Month: month,
Days: days,
Interest: interest,
Balance: balance,
}
return append(s, line)
}
func (s Statement) IncomingBalance() (money.Amount, error) {
if len(s) == 0 {
return money.Amount{}, fmt.Errorf("empty statement")
}
a, err := s[0].Balance.Sub(s[0].Interest)
if err != nil {
return money.Amount{}, err
}
return a, nil
}
func (s Statement) OutgoingBalance() (money.Amount, error) {
if len(s) == 0 {
return money.Amount{}, fmt.Errorf("empty statement")
}
return s[len(s)-1].Balance, nil
}
// PercChange computes (OutgoingBalance - IncomingBalance) / IncomingBalance.
func (s Statement) PercChange() (decimal.Decimal, error) {
inc, err := s.IncomingBalance()
if err != nil {
return decimal.Decimal{}, err
}
out, err := s.OutgoingBalance()
if err != nil {
return decimal.Decimal{}, err
}
diff, err := out.Sub(inc)
if err != nil {
return decimal.Decimal{}, err
}
rat, err := diff.Rat(inc)
if err != nil {
return decimal.Decimal{}, err
}
return rat, nil
}
func (s Statement) TotalInterest() (money.Amount, error) {
if len(s) == 0 {
return money.Amount{}, fmt.Errorf("empty statement")
}
var err error
total := s[0].Interest
for i := 1; i < len(s); i++ {
total, err = total.Add(s[i].Interest)
if err != nil {
return money.Amount{}, err
}
}
return total, nil
}
// DailyRate computes YearlyRate / 365.
func DailyRate(yearlyRate decimal.Decimal) (decimal.Decimal, error) {
daysInYear, err := decimal.New(365, 0)
if err != nil {
return decimal.Decimal{}, err
}
dailyRate, err := yearlyRate.Quo(daysInYear)
if err != nil {
return decimal.Decimal{}, err
}
return dailyRate, nil
}
// MonthlyInterest computes Balance * DailyRate * DaysInMonth.
func MonthlyInterest(balance money.Amount, dailyRate decimal.Decimal, daysInMonth int) (money.Amount, error) {
var err error
interest := balance.Zero()
for i := 0; i < daysInMonth; i++ {
interest, err = balance.FMA(dailyRate, interest)
if err != nil {
return money.Amount{}, err
}
}
interest = interest.RoundToCurr()
return interest, nil
}
func SimulateStatement(balance money.Amount, yearlyRate decimal.Decimal) (Statement, error) {
daysInMonths := [...]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
dailyRate, err := DailyRate(yearlyRate)
if err != nil {
return nil, err
}
statement := Statement{}
for m := 0; m < len(daysInMonths); m++ {
// Compute the interest
interest, err := MonthlyInterest(balance, dailyRate, daysInMonths[m])
if err != nil {
return nil, err
}
// Compound the balance
balance, err = balance.Add(interest)
if err != nil {
return nil, err
}
// Append month
statement = statement.Append(m+1, daysInMonths[m], interest, balance)
}
return statement, nil
}
// This example calculates the effective interest rate for a 10% nominal
// interest rate compounded monthly on a USD 10,000 balance.
func Example_effectiveRate() {
// Set up initial balance and nominal interest rate
initialBalance := money.MustParseAmount("USD", "10000")
nominalRate := decimal.MustParse("0.10")
// Display initial balance and nominal interest rate
fmt.Printf("Initial Balance = %v\n", initialBalance)
fmt.Printf("Nominal Rate = %.2k\n\n", nominalRate)
// Generate the simulated statement for a year
statement, err := SimulateStatement(initialBalance, nominalRate)
if err != nil {
panic(err)
}
// Display monthly balances, including the interest accrued each month
fmt.Printf("%-5s %-5s %-12s %s\n", "Month", "Days", "Interest", "Balance")
for _, line := range statement {
fmt.Printf("%5v %5v %+11f %11f\n", line.Month, line.Days, line.Interest, line.Balance)
}
// Calculate total interest accrued over the year and effective interest rate
totalInterest, err := statement.TotalInterest()
if err != nil {
panic(err)
}
effRate, err := statement.PercChange()
if err != nil {
panic(err)
}
// Display the total interest accrued and the effective interest rate
fmt.Printf(" Total %+11f\n\n", totalInterest)
fmt.Printf("Effective Rate = %.4k\n", effRate)
// Output:
// Initial Balance = USD 10000.00
// Nominal Rate = 10.00%
//
// Month Days Interest Balance
// 1 31 +84.93 10084.93
// 2 28 +77.36 10162.29
// 3 31 +86.31 10248.60
// 4 30 +84.24 10332.84
// 5 31 +87.76 10420.60
// 6 30 +85.65 10506.25
// 7 31 +89.23 10595.48
// 8 31 +89.99 10685.47
// 9 30 +87.83 10773.30
// 10 31 +91.50 10864.80
// 11 30 +89.30 10954.10
// 12 31 +93.03 11047.13
// Total +1047.13
//
// Effective Rate = 10.4713%
}
type ScheduleLine struct {
Month int
Repayment money.Amount
Principal money.Amount
Interest money.Amount
Balance money.Amount
}
type AmortizationSchedule []ScheduleLine
func (p AmortizationSchedule) Append(month int, repayment, principal, interest, balance money.Amount) AmortizationSchedule {
newLine := ScheduleLine{
Month: month,
Repayment: repayment,
Principal: principal,
Interest: interest,
Balance: balance,
}
return append(p, newLine)
}
func (p AmortizationSchedule) TotalRepayment() (money.Amount, error) {
if len(p) == 0 {
return money.Amount{}, fmt.Errorf("empty schedule")
}
var err error
total := p[0].Repayment
for i := 1; i < len(p); i++ {
total, err = total.Add(p[i].Repayment)
if err != nil {
return money.Amount{}, err
}
}
return total, nil
}
func (p AmortizationSchedule) TotalPrincipal() (money.Amount, error) {
if len(p) == 0 {
return money.Amount{}, fmt.Errorf("empty schedule")
}
var err error
total := p[0].Principal
for i := 1; i < len(p); i++ {
total, err = total.Add(p[i].Principal)
if err != nil {
return money.Amount{}, err
}
}
return total, nil
}
func (p AmortizationSchedule) TotalInterest() (money.Amount, error) {
if len(p) == 0 {
return money.Amount{}, fmt.Errorf("empty schedule")
}
var err error
total := p[0].Interest
for i := 1; i < len(p); i++ {
total, err = total.Add(p[i].Interest)
if err != nil {
return money.Amount{}, err
}
}
return total, nil
}
// MonthlyRate computes YearlyRate / 12.
func MonthlyRate(yearlyRate decimal.Decimal) (decimal.Decimal, error) {
monthsInYear, err := decimal.New(12, 0)
if err != nil {
return decimal.Decimal{}, err
}
monthlyRate, err := yearlyRate.Quo(monthsInYear)
if err != nil {
return decimal.Decimal{}, err
}
return monthlyRate, nil
}
// AnnuityPayment computes Amount * Rate / (1 - (1 + Rate)^(-Periods)).
func AnnuityPayment(amount money.Amount, rate decimal.Decimal, periods int) (money.Amount, error) {
one := rate.One()
// Numerator
num, err := amount.Mul(rate)
if err != nil {
return money.Amount{}, err
}
// Denominator
den, err := rate.Add(one)
if err != nil {
return money.Amount{}, err
}
den, err = den.Pow(-periods)
if err != nil {
return money.Amount{}, err
}
den, err = one.Sub(den)
if err != nil {
return money.Amount{}, err
}
res, err := num.Quo(den)
if err != nil {
return money.Amount{}, err
}
return res.RoundToCurr(), nil
}
func SimulateSchedule(balance money.Amount, yearlyRate decimal.Decimal, years int) (AmortizationSchedule, error) {
months := years * 12
monthlyRate, err := MonthlyRate(yearlyRate)
if err != nil {
return nil, err
}
repayment, err := AnnuityPayment(balance, monthlyRate, months)
if err != nil {
return nil, err
}
schedule := AmortizationSchedule{}
for i := 0; i < months; i++ {
interest, err := balance.Mul(monthlyRate)
if err != nil {
return nil, err
}
interest = interest.RoundToCurr()
principal, err := repayment.Sub(interest)
if err != nil {
return nil, err
}
balance, err = balance.Sub(principal)
if err != nil {
return nil, err
}
schedule = schedule.Append(i+1, repayment, principal, interest, balance)
}
return schedule, nil
}
// In this example, a loan amortization table is generated for a loan with
// an initial amount of USD 12,000, an annual interest rate of 10%, and
// a repayment period of 1 year.
func Example_loanAmortization() {
// Set up initial loan balance and interest rate
initialBalance := money.MustParseAmount("USD", "12000")
yearlyRate := decimal.MustParse("0.10")
years := 1
// Display the initial loan balance and interest rate
fmt.Printf("Initial Balance = %v\n", initialBalance)
fmt.Printf("Interest Rate = %.2k\n\n", yearlyRate)
// Generate the amortization schedule
schedule, err := SimulateSchedule(initialBalance, yearlyRate, years)
if err != nil {
panic(err)
}
// Display the amortization schedule, showing the monthly
// repayment, principal, interest and outstanding loan balance
fmt.Println("Month Repayment Principal Interest Outstanding")
for _, line := range schedule {
fmt.Printf("%5d %12f %11f %11f %11f\n", line.Month, line.Repayment, line.Principal, line.Interest, line.Balance)
}
// Calculate and display the total amounts repaid, both principal and interest
totalRepayment, err := schedule.TotalRepayment()
if err != nil {
panic(err)
}
totalPrincipal, err := schedule.TotalPrincipal()
if err != nil {
panic(err)
}
totalInterest, err := schedule.TotalInterest()
if err != nil {
panic(err)
}
// Display the total repayment, principal repayment and interest payment over the loan period
fmt.Printf("Total %12f %11f %11f\n", totalRepayment, totalPrincipal, totalInterest)
// Output:
// Initial Balance = USD 12000.00
// Interest Rate = 10.00%
//
// Month Repayment Principal Interest Outstanding
// 1 1054.99 954.99 100.00 11045.01
// 2 1054.99 962.95 92.04 10082.06
// 3 1054.99 970.97 84.02 9111.09
// 4 1054.99 979.06 75.93 8132.03
// 5 1054.99 987.22 67.77 7144.81
// 6 1054.99 995.45 59.54 6149.36
// 7 1054.99 1003.75 51.24 5145.61
// 8 1054.99 1012.11 42.88 4133.50
// 9 1054.99 1020.54 34.45 3112.96
// 10 1054.99 1029.05 25.94 2083.91
// 11 1054.99 1037.62 17.37 1046.29
// 12 1054.99 1046.27 8.72 0.02
// Total 12659.88 11999.98 659.90
}
func FromISO8583(s string) (money.Amount, error) {
// Amount
n, err := strconv.ParseInt(s[4:], 10, 64)
if err != nil {
return money.Amount{}, err
}
a, err := money.NewAmountFromMinorUnits(s[:3], n)
if err != nil {
return money.Amount{}, err
}
// Sign
if s[3:4] == "D" {
a = a.Neg()
}
return a, nil
}
// In this example, we parse the string "840D000000001234", which represents -12.34 USD,
// according to the specification for "DE54, Additional Amounts" in ISO 8583.
func Example_parsingISO8583() {
a, _ := FromISO8583("840D000000001234")
fmt.Println(a)
// Output:
// USD -12.34
}
func FromMoneyProto(curr string, units int64, nanos int32) (money.Amount, error) {
return money.NewAmountFromInt64(curr, units, int64(nanos), 9)
}
func ToMoneyProto(a money.Amount) (curr string, units int64, nanos int32, ok bool) {
curr = a.Curr().Code()
whole, frac, ok := a.Int64(9)
return curr, whole, int32(frac), ok
}
// This is an example of how to a parse a monetary amount formatted as [money.proto].
//
// [money.proto]: https://github.com/googleapis/googleapis/blob/master/google/type/money.proto
func Example_parsingProtobuf() {
a, _ := FromMoneyProto("USD", 5, 670000000)
fmt.Println(a)
fmt.Println(ToMoneyProto(a))
// Output:
// USD 5.67
// USD 5 670000000 true
}
func FromStripe(curr string, units int64) (money.Amount, error) {
return money.NewAmountFromMinorUnits(curr, units)
}
func ToStripe(a money.Amount) (curr string, units int64, ok bool) {
curr = strings.ToLower(a.Curr().Code())
units, ok = a.MinorUnits()
return curr, units, ok
}
// This is an example of how to a parse a monetary amount
// formatted according to [Stripe API] specification.
//
// [Stripe API]: https://stripe.com/docs/api/balance/balance_object
func Example_parsingStripe() {
a, _ := FromStripe("usd", 567)
fmt.Println(a)
fmt.Println(ToStripe(a))
// Output:
// USD 5.67
// usd 567 true
}
func ExampleParseCurr_currencies() {
fmt.Println(money.ParseCurr("JPY"))
fmt.Println(money.ParseCurr("USD"))
fmt.Println(money.ParseCurr("OMR"))
// Output:
// JPY <nil>
// USD <nil>
// OMR <nil>
}
func ExampleParseCurr_codes() {
fmt.Println(money.ParseCurr("usd"))
fmt.Println(money.ParseCurr("USD"))
fmt.Println(money.ParseCurr("840"))
// Output:
// USD <nil>
// USD <nil>
// USD <nil>
}
func ExampleMustParseCurr_currencies() {
fmt.Println(money.MustParseCurr("JPY"))
fmt.Println(money.MustParseCurr("USD"))
fmt.Println(money.MustParseCurr("OMR"))
// Output:
// JPY
// USD
// OMR
}
func ExampleMustParseCurr_codes() {
fmt.Println(money.MustParseCurr("usd"))
fmt.Println(money.MustParseCurr("USD"))
fmt.Println(money.MustParseCurr("840"))
// Output:
// USD
// USD
// USD
}
func ExampleCurrency_String() {
c := money.USD
fmt.Println(c.String())
// Output: USD
}
func ExampleCurrency_Code() {
j := money.JPY
u := money.USD
o := money.OMR
fmt.Println(j.Code())
fmt.Println(u.Code())
fmt.Println(o.Code())
// Output:
// JPY
// USD
// OMR
}
func ExampleCurrency_Num() {
j := money.JPY
u := money.USD
o := money.OMR
fmt.Println(j.Num())
fmt.Println(u.Num())
fmt.Println(o.Num())
// Output:
// 392
// 840
// 512
}
func ExampleCurrency_Scale() {
j := money.JPY
u := money.USD
o := money.OMR
fmt.Println(j.Scale())
fmt.Println(u.Scale())
fmt.Println(o.Scale())
// Output:
// 0
// 2
// 3
}
type Object struct {
Currency money.Currency `json:"currency"`
}
func ExampleCurrency_UnmarshalText_json() {
var v Object
_ = json.Unmarshal([]byte(`{"currency":"USD"}`), &v)
fmt.Println(v)
// Output: {USD}
}
func ExampleCurrency_MarshalText_json() {
v := Object{
Currency: money.USD,
}
b, _ := json.Marshal(v)
fmt.Println(string(b))
// Output: {"currency":"USD"}
}
type Entity struct {
Currency money.Currency `xml:"Currency"`
}
func ExampleCurrency_UnmarshalText_xml() {
var v Entity
_ = xml.Unmarshal([]byte(`<Entity><Currency>USD</Currency></Entity>`), &v)
fmt.Println(v)
// Output: {USD}
}
func ExampleCurrency_MarshalText_xml() {
v := Entity{
Currency: money.USD,
}
b, _ := xml.Marshal(v)
fmt.Println(string(b))
// Output: <Entity><Currency>USD</Currency></Entity>
}
func ExampleCurrency_Scan() {
u := money.XXX
_ = u.Scan("USD")
fmt.Println(u)
// Output: USD
}
func ExampleCurrency_Value() {
u := money.USD
fmt.Println(u.Value())
// Output: USD <nil>
}
func ExampleCurrency_Format() {
fmt.Printf("%c\n", money.USD)
// Output:
// USD
}
func ExampleNullCurrency_Scan() {
var n, m money.NullCurrency
_ = n.Scan("USD")
_ = m.Scan(nil)
fmt.Println(n)
fmt.Println(m)
// Output:
// {USD true}
// {XXX false}
}
func ExampleNullCurrency_Value() {
n := money.NullCurrency{
Currency: money.USD,
Valid: true,
}
m := money.NullCurrency{
Currency: money.XXX,
Valid: false,
}
fmt.Println(n.Value())
fmt.Println(m.Value())
// Output:
// USD <nil>
// <nil> <nil>
}
func ExampleNewAmount_scales() {
fmt.Println(money.NewAmount("USD", 567, 0))
fmt.Println(money.NewAmount("USD", 567, 1))
fmt.Println(money.NewAmount("USD", 567, 2))
fmt.Println(money.NewAmount("USD", 567, 3))
fmt.Println(money.NewAmount("USD", 567, 4))
// Output:
// USD 567.00 <nil>
// USD 56.70 <nil>
// USD 5.67 <nil>
// USD 0.567 <nil>
// USD 0.0567 <nil>
}
func ExampleNewAmount_currencies() {
fmt.Println(money.NewAmount("JPY", 567, 2))
fmt.Println(money.NewAmount("USD", 567, 2))
fmt.Println(money.NewAmount("OMR", 567, 2))
// Output:
// JPY 5.67 <nil>
// USD 5.67 <nil>
// OMR 5.670 <nil>
}
func ExampleMustNewAmount_scales() {
fmt.Println(money.MustNewAmount("USD", 567, 0))
fmt.Println(money.MustNewAmount("USD", 567, 1))
fmt.Println(money.MustNewAmount("USD", 567, 2))
fmt.Println(money.MustNewAmount("USD", 567, 3))
fmt.Println(money.MustNewAmount("USD", 567, 4))
// Output:
// USD 567.00
// USD 56.70
// USD 5.67
// USD 0.567
// USD 0.0567
}
func ExampleMustNewAmount_currencies() {
fmt.Println(money.MustNewAmount("JPY", 567, 2))
fmt.Println(money.MustNewAmount("USD", 567, 2))
fmt.Println(money.MustNewAmount("OMR", 567, 2))
// Output:
// JPY 5.67
// USD 5.67
// OMR 5.670
}
func ExampleNewAmountFromDecimal() {
d := decimal.MustParse("5.67")
fmt.Println(money.NewAmountFromDecimal(money.JPY, d))
fmt.Println(money.NewAmountFromDecimal(money.USD, d))
fmt.Println(money.NewAmountFromDecimal(money.OMR, d))
// Output:
// JPY 5.67 <nil>
// USD 5.67 <nil>
// OMR 5.670 <nil>
}
func ExampleNewAmountFromInt64_scales() {
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 2))
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 3))
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 4))
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 5))
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 6))
// Output:
// USD 5.67 <nil>
// USD 5.067 <nil>
// USD 5.0067 <nil>
// USD 5.00067 <nil>
// USD 5.000067 <nil>
}
func ExampleNewAmountFromInt64_currencies() {
fmt.Println(money.NewAmountFromInt64("JPY", 5, 67, 2))
fmt.Println(money.NewAmountFromInt64("USD", 5, 67, 2))
fmt.Println(money.NewAmountFromInt64("OMR", 5, 67, 2))
// Output:
// JPY 5.67 <nil>
// USD 5.67 <nil>
// OMR 5.670 <nil>
}
func ExampleNewAmountFromFloat64_currencies() {
fmt.Println(money.NewAmountFromFloat64("JPY", 5.67e0))
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e0))
fmt.Println(money.NewAmountFromFloat64("OMR", 5.67e0))
// Output:
// JPY 5.67 <nil>
// USD 5.67 <nil>
// OMR 5.670 <nil>
}
func ExampleNewAmountFromFloat64_scales() {
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e-2))
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e-1))
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e0))
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e1))
fmt.Println(money.NewAmountFromFloat64("USD", 5.67e2))
// Output:
// USD 0.0567 <nil>
// USD 0.567 <nil>
// USD 5.67 <nil>
// USD 56.70 <nil>
// USD 567.00 <nil>
}
func ExampleNewAmountFromMinorUnits_currencies() {
fmt.Println(money.NewAmountFromMinorUnits("JPY", 567))
fmt.Println(money.NewAmountFromMinorUnits("USD", 567))
fmt.Println(money.NewAmountFromMinorUnits("OMR", 567))
// Output:
// JPY 567 <nil>
// USD 5.67 <nil>
// OMR 0.567 <nil>
}
func ExampleNewAmountFromMinorUnits_scales() {
fmt.Println(money.NewAmountFromMinorUnits("USD", 5))
fmt.Println(money.NewAmountFromMinorUnits("USD", 56))
fmt.Println(money.NewAmountFromMinorUnits("USD", 567))
fmt.Println(money.NewAmountFromMinorUnits("USD", 5670))
fmt.Println(money.NewAmountFromMinorUnits("USD", 56700))
// Output:
// USD 0.05 <nil>
// USD 0.56 <nil>
// USD 5.67 <nil>
// USD 56.70 <nil>
// USD 567.00 <nil>
}
func ExampleMustParseAmount_currencies() {
fmt.Println(money.MustParseAmount("JPY", "5.67"))
fmt.Println(money.MustParseAmount("USD", "5.67"))
fmt.Println(money.MustParseAmount("OMR", "5.67"))
// Output:
// JPY 5.67
// USD 5.67
// OMR 5.670
}
func ExampleMustParseAmount_scales() {
fmt.Println(money.MustParseAmount("USD", "0.0567"))
fmt.Println(money.MustParseAmount("USD", "0.567"))
fmt.Println(money.MustParseAmount("USD", "5.67"))
fmt.Println(money.MustParseAmount("USD", "56.7"))
fmt.Println(money.MustParseAmount("USD", "567"))
// Output:
// USD 0.0567
// USD 0.567
// USD 5.67
// USD 56.70
// USD 567.00
}
func ExampleParseAmount_currencies() {
fmt.Println(money.ParseAmount("JPY", "5.67"))
fmt.Println(money.ParseAmount("USD", "5.67"))
fmt.Println(money.ParseAmount("OMR", "5.67"))
// Output:
// JPY 5.67 <nil>
// USD 5.67 <nil>
// OMR 5.670 <nil>
}
func ExampleParseAmount_scales() {
fmt.Println(money.ParseAmount("USD", "0.0567"))
fmt.Println(money.ParseAmount("USD", "0.567"))
fmt.Println(money.ParseAmount("USD", "5.67"))
fmt.Println(money.ParseAmount("USD", "56.7"))
fmt.Println(money.ParseAmount("USD", "567"))
// Output:
// USD 0.0567 <nil>
// USD 0.567 <nil>
// USD 5.67 <nil>
// USD 56.70 <nil>
// USD 567.00 <nil>
}
func ExampleAmount_MinorUnits_currencies() {
a := money.MustParseAmount("JPY", "5.678")
b := money.MustParseAmount("USD", "5.678")
c := money.MustParseAmount("OMR", "5.678")
fmt.Println(a.MinorUnits())
fmt.Println(b.MinorUnits())
fmt.Println(c.MinorUnits())
// Output:
// 6 true
// 568 true
// 5678 true
}
func ExampleAmount_MinorUnits_scales() {
a := money.MustParseAmount("USD", "0.0567")
b := money.MustParseAmount("USD", "0.567")
c := money.MustParseAmount("USD", "5.67")
d := money.MustParseAmount("USD", "56.7")
e := money.MustParseAmount("USD", "567")
fmt.Println(a.MinorUnits())
fmt.Println(b.MinorUnits())
fmt.Println(c.MinorUnits())
fmt.Println(d.MinorUnits())
fmt.Println(e.MinorUnits())
// Output:
// 6 true
// 57 true
// 567 true
// 5670 true
// 56700 true
}
func ExampleAmount_Float64() {
a := money.MustParseAmount("USD", "0.10")
b := money.MustParseAmount("USD", "123.456")
c := money.MustParseAmount("USD", "1234567890.123456789")
fmt.Println(a.Float64())
fmt.Println(b.Float64())
fmt.Println(c.Float64())
// Output:
// 0.1 true
// 123.456 true
// 1.2345678901234567e+09 true
}
func ExampleAmount_Int64() {
a := money.MustParseAmount("USD", "5.678")
fmt.Println(a.Int64(0))
fmt.Println(a.Int64(1))
fmt.Println(a.Int64(2))
fmt.Println(a.Int64(3))
fmt.Println(a.Int64(4))
// Output:
// 6 0 true
// 5 7 true
// 5 68 true
// 5 678 true
// 5 6780 true
}
func ExampleAmount_Curr() {
a := money.MustParseAmount("USD", "5.67")
fmt.Println(a.Curr())
// Output: USD
}
func ExampleAmount_Decimal() {
a := money.MustParseAmount("USD", "5.67")
fmt.Println(a.Decimal())
// Output: 5.67
}
func ExampleAmount_Add() {
a := money.MustParseAmount("USD", "5.67")
b := money.MustParseAmount("USD", "23.00")
fmt.Println(a.Add(b))
// Output: USD 28.67 <nil>
}
func ExampleAmount_Sub() {
a := money.MustParseAmount("USD", "5.67")
b := money.MustParseAmount("USD", "23.00")
fmt.Println(a.Sub(b))
// Output: USD -17.33 <nil>
}
func ExampleAmount_SubAbs() {
a := money.MustParseAmount("USD", "5.67")
b := money.MustParseAmount("USD", "23.00")
fmt.Println(a.SubAbs(b))
// Output: USD 17.33 <nil>
}
func ExampleAmount_FMA() {
a := money.MustParseAmount("USD", "5.67")
b := money.MustParseAmount("USD", "23.00")
e := decimal.MustParse("2")
fmt.Println(a.FMA(e, b))
// Output: USD 34.34 <nil>
}
func ExampleAmount_Mul() {
a := money.MustParseAmount("USD", "5.67")
e := decimal.MustParse("2")
fmt.Println(a.Mul(e))
// Output: USD 11.34 <nil>
}
func ExampleAmount_Quo() {
a := money.MustParseAmount("USD", "5.67")
e := decimal.MustParse("2")
fmt.Println(a.Quo(e))
// Output: USD 2.835 <nil>
}
func ExampleAmount_QuoRem() {
a := money.MustParseAmount("JPY", "5.67")
b := money.MustParseAmount("USD", "5.67")
c := money.MustParseAmount("OMR", "5.67")
e := decimal.MustParse("2")
fmt.Println(a.QuoRem(e))
fmt.Println(b.QuoRem(e))
fmt.Println(c.QuoRem(e))
// Output:
// JPY 2 JPY 1.67 <nil>
// USD 2.83 USD 0.01 <nil>
// OMR 2.835 OMR 0.000 <nil>
}
func ExampleAmount_Split_scales() {
a := money.MustParseAmount("USD", "0.0567")
b := money.MustParseAmount("USD", "0.567")
c := money.MustParseAmount("USD", "5.67")
d := money.MustParseAmount("USD", "56.7")
e := money.MustParseAmount("USD", "567")
fmt.Println(a.Split(2))
fmt.Println(b.Split(2))
fmt.Println(c.Split(2))
fmt.Println(d.Split(2))