-
Notifications
You must be signed in to change notification settings - Fork 1
/
money.go
144 lines (119 loc) · 2.63 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
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
package money
import (
"fmt"
"math"
"strings"
)
const (
decimalDefault = "."
)
// Money represents an amount ot a specific currency
type Money struct {
amount float64
currency Currency
}
// New creates a new Money object
func New(f float64, c Currency) *Money {
m := &Money{currency: c}
m.amount = m.round(f)
return m
}
// Amount returns the numerical value of the money
func (m Money) Amount() float64 {
return m.amount
}
// CurrencyCode returns the code of currency
func (m Money) CurrencyCode() string {
return string(m.currency)
}
// Currency returns the string representation of currency
func (m Money) Currency() string {
d := currencies[m.currency]
value := m.Format(d.thousand, d.decimal)
return fmt.Sprintf("%s %s", d.symbol, value)
}
// Symbol returns the currency symbom
func (m Money) Symbol(c Currency) string {
return currencies[c].symbol
}
// Format returns the formatted value according rules
func (m Money) Format(thousand, decimal byte) string {
if thousand == 0 {
thousand = ','
}
if decimal == 0 {
decimal = '.'
}
str := fmt.Sprintf("%.2f", m.amount)
arr := strings.Split(str, decimalDefault)
n := arr[0]
var bytes []byte
l := len(n) - 1
for i, j := l, 1; i >= 0; i-- {
if j > 3 {
j = 1
bytes = append(bytes, thousand)
}
j++
bytes = append(bytes, n[i])
}
var res string
for _, e := range bytes {
res = string(e) + res
}
return fmt.Sprintf("%s%s%s", res, string(decimal), arr[1])
}
// Formmated returns the formatted value according currency
func (m Money) Formatted() string {
d := currencies[m.currency]
return m.Format(d.thousand, d.decimal)
}
// Absolute returns the absolute value of current amount
func (m *Money) Absolute() float64 {
return math.Abs(m.amount)
}
func (m Money) Compare(v float64) int {
v = m.round(v)
if m.amount < v {
return -1
}
if m.amount > v {
return 1
}
return 0
}
func (m Money) Equals(v float64) bool {
v = m.round(v)
return m.Compare(v) == 0
}
func (m Money) GreaterThan(v float64) bool {
v = m.round(v)
return m.Compare(v) > 0
}
func (m Money) GreaterThanOrEqual(v float64) bool {
v = m.round(v)
return m.Compare(v) >= 0
}
func (m Money) LessThan(v float64) bool {
v = m.round(v)
return m.Compare(v) < 0
}
func (m Money) LessThanOrEqual(v float64) bool {
v = m.round(v)
return m.Compare(v) <= 0
}
func (m *Money) Subtract(v float64) float64 {
v = m.round(v)
m.amount -= v
return m.amount
}
func (m *Money) Sum(v float64) float64 {
v = m.round(v)
m.amount += v
return m.amount
}
func (m Money) round(v float64) float64 {
precision := currencies[m.currency].precision
base := math.Pow10(precision)
return math.Round(v*base) / base
}