Skip to content

Commit 6b62d08

Browse files
unkiwiiRhymond
andauthored
Fix ARS decimal and thousand separators and add testable examples (#81)
* Fix ARS decimal and thousand separators * Add testable examples Also fixed README.md examples that were outdated Co-authored-by: Raymond <[email protected]> Co-authored-by: Raymond <[email protected]>
1 parent aa7f5f8 commit 6b62d08

File tree

2 files changed

+188
-4
lines changed

2 files changed

+188
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ This package provides basic and precise Money operations such as rounding, split
1313
```go
1414
package main
1515

16-
import "github.com/Rhymond/go-money"
16+
import (
17+
"log"
18+
19+
"github.com/Rhymond/go-money"
20+
)
1721

1822
func main() {
1923
pound := money.New(100, money.GBP)
@@ -89,7 +93,7 @@ To assert if Money value is equal to zero use `IsZero()`
8993

9094
```go
9195
pound := money.New(100, money.GBP)
92-
result := pound.IsZero(pound) // false
96+
result := pound.IsZero() // false
9397
```
9498

9599
#### Positive value
@@ -98,7 +102,7 @@ To assert if Money value is more than zero use `IsPositive()`
98102

99103
```go
100104
pound := money.New(100, money.GBP)
101-
pound.IsPositive(pound) // true
105+
pound.IsPositive() // true
102106
```
103107

104108
#### Negative value
@@ -107,7 +111,7 @@ To assert if Money value is less than zero use `IsNegative()`
107111

108112
```go
109113
pound := money.New(100, money.GBP)
110-
pound.IsNegative(pound) // false
114+
pound.IsNegative() // false
111115
```
112116

113117
Operations

money_example_test.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package money_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/Rhymond/go-money"
8+
)
9+
10+
func ExampleMoney() {
11+
pound := money.New(100, "GBP")
12+
twoPounds, err := pound.Add(pound)
13+
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
18+
parties, err := twoPounds.Split(3)
19+
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
fmt.Println(parties[0].Display())
25+
fmt.Println(parties[1].Display())
26+
fmt.Println(parties[2].Display())
27+
28+
// Output:
29+
// £0.67
30+
// £0.67
31+
// £0.66
32+
}
33+
34+
func ExampleNew() {
35+
pound := money.New(100, "GBP")
36+
37+
fmt.Println(pound.Display())
38+
39+
// Output:
40+
// £1.00
41+
}
42+
43+
func ExampleMoney_comparisons() {
44+
pound := money.New(100, "GBP")
45+
twoPounds := money.New(200, "GBP")
46+
twoEuros := money.New(200, "EUR")
47+
48+
gt, err := pound.GreaterThan(twoPounds)
49+
fmt.Println(gt, err)
50+
51+
lt, err := pound.LessThan(twoPounds)
52+
fmt.Println(lt, err)
53+
54+
eq, err := twoPounds.Equals(twoEuros)
55+
fmt.Println(eq, err)
56+
57+
// Output:
58+
// false <nil>
59+
// true <nil>
60+
// false currencies don't match
61+
}
62+
63+
func ExampleMoney_IsZero() {
64+
pound := money.New(100, "GBP")
65+
fmt.Println(pound.IsZero())
66+
67+
// Output:
68+
// false
69+
}
70+
71+
func ExampleMoney_IsPositive() {
72+
pound := money.New(100, "GBP")
73+
fmt.Println(pound.IsPositive())
74+
75+
// Output:
76+
// true
77+
}
78+
79+
func ExampleMoney_IsNegative() {
80+
pound := money.New(100, "GBP")
81+
fmt.Println(pound.IsNegative())
82+
83+
// Output:
84+
// false
85+
}
86+
87+
func ExampleMoney_Add() {
88+
pound := money.New(100, "GBP")
89+
twoPounds := money.New(200, "GBP")
90+
91+
result, err := pound.Add(twoPounds)
92+
fmt.Println(result.Display(), err)
93+
94+
// Output:
95+
// £3.00 <nil>
96+
}
97+
98+
func ExampleMoney_Subtract() {
99+
pound := money.New(100, "GBP")
100+
twoPounds := money.New(200, "GBP")
101+
102+
result, err := pound.Subtract(twoPounds)
103+
fmt.Println(result.Display(), err)
104+
105+
// Output:
106+
// -£1.00 <nil>
107+
}
108+
109+
func ExampleMoney_Multiply() {
110+
pound := money.New(100, "GBP")
111+
112+
result := pound.Multiply(2)
113+
fmt.Println(result.Display())
114+
115+
// Output:
116+
// £2.00
117+
}
118+
119+
func ExampleMoney_Absolute() {
120+
pound := money.New(-100, "GBP")
121+
122+
result := pound.Absolute()
123+
fmt.Println(result.Display())
124+
125+
// Output:
126+
// £1.00
127+
}
128+
129+
func ExampleMoney_Split() {
130+
pound := money.New(100, "GBP")
131+
parties, err := pound.Split(3)
132+
133+
if err != nil {
134+
log.Fatal(err)
135+
}
136+
137+
fmt.Println(parties[0].Display())
138+
fmt.Println(parties[1].Display())
139+
fmt.Println(parties[2].Display())
140+
141+
// Output:
142+
// £0.34
143+
// £0.33
144+
// £0.33
145+
}
146+
147+
func ExampleMoney_Allocate() {
148+
pound := money.New(100, "GBP")
149+
// Allocate is variadic function which can receive ratios as
150+
// slice (int[]{33, 33, 33}...) or separated by a comma integers
151+
parties, err := pound.Allocate(33, 33, 33)
152+
153+
if err != nil {
154+
log.Fatal(err)
155+
}
156+
157+
fmt.Println(parties[0].Display())
158+
fmt.Println(parties[1].Display())
159+
fmt.Println(parties[2].Display())
160+
161+
// Output:
162+
// £0.34
163+
// £0.33
164+
// £0.33
165+
}
166+
167+
func ExampleMoney_Display() {
168+
fmt.Println(money.New(123456789, "EUR").Display())
169+
170+
// Output:
171+
// €1,234,567.89
172+
}
173+
174+
func ExampleMoney_AsMajorUnits() {
175+
result := money.New(123456789, "EUR").AsMajorUnits()
176+
fmt.Printf("%.2f", result)
177+
178+
// Output:
179+
// 1234567.89
180+
}

0 commit comments

Comments
 (0)