This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
99 lines (90 loc) · 2.65 KB
/
money.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
package money
// Money represents an amount of money for a currency
//
// Money is any item or verifiable record that is generally accepted as payment
// for goods and services and repayment of debts
type Money struct {
Amount Decimal `json:"amount"`
Currency Currency `json:"currency"`
}
// MustParse is like Parse, but panics if the given amount or currency cannot
// be parsed. It simplifies safe initialisation of Money values.
func MustParse(amount, currency string) *Money {
m, err := Parse(amount, currency)
if err != nil {
panic(err)
}
return m
}
// Parse parses amount which must contain a text representation of a
// floating-point number.
// The number of integers after the radix point (fraction) determines the
// mantissa precision.
//
// e.g. 120.0 -> Precision 1
// e.g. 123.456 -> Precision 3
//
// It also validates the currency, which must represented in code as defined by
// the ISO 4217 format.
//
// e.g. CHF -> Swiss franc
func Parse(amount, currency string) (*Money, error) {
a, err := ParseDecimal(amount)
if err != nil {
return nil, err
}
c, err := ParseCurrency(currency)
if err != nil {
return nil, err
}
return &Money{
Amount: a,
Currency: c,
}, nil
}
// Equal tests whether y equal x. When the currency is different, it will
// always return false. Currency conversion is currently not supported.
func (x *Money) Equal(y *Money) bool {
if x.Currency != y.Currency {
return false
}
return x.Amount.Equal(y.Amount)
}
// Validate tests that both the decimal and the currency are valid
func (x *Money) Validate() error {
if err := x.Currency.Validate(); err != nil {
return err
}
return x.Amount.Validate()
}
// Add returns an amount set to the rounded sum x+y.
// The precision is set to the larger of x's or y's precision before the
// operation.
// Rounding is performed according to the default rounding mode
func Add(x, y *Money) *Money {
z := Money{}
return &z
}
// Sub returns an amount set to the rounded difference x-y.
// Precision, rounding, and accuracy reporting are as for Add.
// Sub panics with ErrNaN if x and y are infinities with equal
// signs.
func Sub(x, y *Money) *Money {
z := Money{}
return &z
}
// Mul sets z to the rounded product x*y and returns z.
// Precision, rounding, and accuracy reporting are as for Add.
// Mul panics with ErrNaN if one operand is zero and the other
// operand an infinity.
func Mul(x, y *Money) *Money {
z := Money{}
return &z
}
// Div sets z to the rounded quotient x/y and returns z.
// Precision, rounding, and accuracy reporting are as for Add.
// Quo panics with ErrNaN if both operands are zero or infinities.
func Div(x, y *Money) *Money {
z := Money{}
return &z
}