From 28cf971e12c2895978d48ca0cc30bce3a5e009ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20G=2E=20S=C3=A1nchez?= Date: Fri, 19 Mar 2021 10:54:16 -0300 Subject: [PATCH 1/2] Fix ARS decimal and thousand separators --- currency.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/currency.go b/currency.go index 05c9820..00daf08 100644 --- a/currency.go +++ b/currency.go @@ -23,7 +23,7 @@ var currencies = map[string]*Currency{ "AMD": {Decimal: ".", Thousand: ",", Code: "AMD", Fraction: 2, NumericCode: "051", Grapheme: "\u0564\u0580.", Template: "1 $"}, "ANG": {Decimal: ",", Thousand: ".", Code: "ANG", Fraction: 2, NumericCode: "532", Grapheme: "\u0192", Template: "$1"}, "AOA": {Decimal: ".", Thousand: ",", Code: "AOA", Fraction: 2, NumericCode: "973", Grapheme: "Kz", Template: "1$"}, - "ARS": {Decimal: ".", Thousand: ",", Code: "ARS", Fraction: 2, NumericCode: "032", Grapheme: "$", Template: "$1"}, + "ARS": {Decimal: ",", Thousand: ".", Code: "ARS", Fraction: 2, NumericCode: "032", Grapheme: "$", Template: "$1"}, "AUD": {Decimal: ".", Thousand: ",", Code: "AUD", Fraction: 2, NumericCode: "036", Grapheme: "$", Template: "$1"}, "AWG": {Decimal: ".", Thousand: ",", Code: "AWG", Fraction: 2, NumericCode: "533", Grapheme: "\u0192", Template: "1$"}, "AZN": {Decimal: ".", Thousand: ",", Code: "AZN", Fraction: 2, NumericCode: "944", Grapheme: "\u20bc", Template: "$1"}, From 414c80277ecb1b613f8fa429f78fcc66cacaeef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20G=2E=20S=C3=A1nchez?= Date: Fri, 19 Mar 2021 11:40:09 -0300 Subject: [PATCH 2/2] Add testable examples Also fixed README.md examples that were outdated --- README.md | 12 ++- money_example_test.go | 180 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 money_example_test.go diff --git a/README.md b/README.md index cab8746..930b203 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,11 @@ This package provides basic and precise Money operations such as rounding, split ```go package main -import "github.com/Rhymond/go-money" +import ( + "log" + + "github.com/Rhymond/go-money" +) func main() { pound := money.New(100, "GBP") @@ -90,7 +94,7 @@ To assert if Money value is equal to zero use `IsZero()` ```go pound := money.New(100, "GBP") -result := pound.IsZero(pound) // false +result := pound.IsZero() // false ``` #### Positive value @@ -99,7 +103,7 @@ To assert if Money value is more than zero use `IsPositive()` ```go pound := money.New(100, "GBP") -pound.IsPositive(pound) // true +pound.IsPositive() // true ``` #### Negative value @@ -108,7 +112,7 @@ To assert if Money value is less than zero use `IsNegative()` ```go pound := money.New(100, "GBP") -pound.IsNegative(pound) // false +pound.IsNegative() // false ``` Operations diff --git a/money_example_test.go b/money_example_test.go new file mode 100644 index 0000000..8e948dd --- /dev/null +++ b/money_example_test.go @@ -0,0 +1,180 @@ +package money_test + +import ( + "fmt" + "log" + + "github.com/Rhymond/go-money" +) + +func ExampleMoney() { + pound := money.New(100, "GBP") + twoPounds, err := pound.Add(pound) + + if err != nil { + log.Fatal(err) + } + + parties, err := twoPounds.Split(3) + + if err != nil { + log.Fatal(err) + } + + fmt.Println(parties[0].Display()) + fmt.Println(parties[1].Display()) + fmt.Println(parties[2].Display()) + + // Output: + // £0.67 + // £0.67 + // £0.66 +} + +func ExampleNew() { + pound := money.New(100, "GBP") + + fmt.Println(pound.Display()) + + // Output: + // £1.00 +} + +func ExampleMoney_comparisons() { + pound := money.New(100, "GBP") + twoPounds := money.New(200, "GBP") + twoEuros := money.New(200, "EUR") + + gt, err := pound.GreaterThan(twoPounds) + fmt.Println(gt, err) + + lt, err := pound.LessThan(twoPounds) + fmt.Println(lt, err) + + eq, err := twoPounds.Equals(twoEuros) + fmt.Println(eq, err) + + // Output: + // false + // true + // false currencies don't match +} + +func ExampleMoney_IsZero() { + pound := money.New(100, "GBP") + fmt.Println(pound.IsZero()) + + // Output: + // false +} + +func ExampleMoney_IsPositive() { + pound := money.New(100, "GBP") + fmt.Println(pound.IsPositive()) + + // Output: + // true +} + +func ExampleMoney_IsNegative() { + pound := money.New(100, "GBP") + fmt.Println(pound.IsNegative()) + + // Output: + // false +} + +func ExampleMoney_Add() { + pound := money.New(100, "GBP") + twoPounds := money.New(200, "GBP") + + result, err := pound.Add(twoPounds) + fmt.Println(result.Display(), err) + + // Output: + // £3.00 +} + +func ExampleMoney_Subtract() { + pound := money.New(100, "GBP") + twoPounds := money.New(200, "GBP") + + result, err := pound.Subtract(twoPounds) + fmt.Println(result.Display(), err) + + // Output: + // -£1.00 +} + +func ExampleMoney_Multiply() { + pound := money.New(100, "GBP") + + result := pound.Multiply(2) + fmt.Println(result.Display()) + + // Output: + // £2.00 +} + +func ExampleMoney_Absolute() { + pound := money.New(-100, "GBP") + + result := pound.Absolute() + fmt.Println(result.Display()) + + // Output: + // £1.00 +} + +func ExampleMoney_Split() { + pound := money.New(100, "GBP") + parties, err := pound.Split(3) + + if err != nil { + log.Fatal(err) + } + + fmt.Println(parties[0].Display()) + fmt.Println(parties[1].Display()) + fmt.Println(parties[2].Display()) + + // Output: + // £0.34 + // £0.33 + // £0.33 +} + +func ExampleMoney_Allocate() { + pound := money.New(100, "GBP") + // Allocate is variadic function which can receive ratios as + // slice (int[]{33, 33, 33}...) or separated by a comma integers + parties, err := pound.Allocate(33, 33, 33) + + if err != nil { + log.Fatal(err) + } + + fmt.Println(parties[0].Display()) + fmt.Println(parties[1].Display()) + fmt.Println(parties[2].Display()) + + // Output: + // £0.34 + // £0.33 + // £0.33 +} + +func ExampleMoney_Display() { + fmt.Println(money.New(123456789, "EUR").Display()) + + // Output: + // €1,234,567.89 +} + +func ExampleMoney_AsMajorUnits() { + result := money.New(123456789, "EUR").AsMajorUnits() + fmt.Printf("%.2f", result) + + // Output: + // 1234567.89 +}