-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.go
256 lines (216 loc) · 5.54 KB
/
currency.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
//
// lurkcoin
// Copyright © 2020 by luk3yx
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package lurkcoin
import (
"errors"
"math/big"
"strings"
)
// Create a custom Currency type that stores read-only currency values.
type Currency struct {
raw *big.Int
}
var i0 = big.NewInt(0)
var i10 = big.NewInt(10)
var i100 = big.NewInt(100)
// A method to convert currency to a string.
func (self Currency) RawString() string {
// This should probably be improved.
whole := new(big.Int)
frac := new(big.Int)
var res string
if self.raw.Cmp(i0) >= 0 {
whole.DivMod(self.raw, i100, frac)
res = whole.String()
} else {
whole.DivMod(new(big.Int).Abs(self.raw), i100, frac)
res = "-" + whole.String()
}
res += "."
if frac.Cmp(i10) < 0 {
res += "0"
} else if frac.Cmp(i100) >= 0 {
panic("Unreachable code (big.Int DivMod did something it shouldn't).")
}
return res + frac.String()
}
// Returns the currency as a human-readable string.
func (self Currency) String() string {
raw := self.RawString()
var builder strings.Builder
// Add the leading ¤.
s := 0
if raw[0] == '-' {
s = 1
builder.WriteByte('-')
}
builder.WriteString(SYMBOL)
// Insert a comma when required
// 123456.78 → 123,456.78
l := len(raw) - 3
for i := s; i < len(raw); i++ {
if l > i && i > s && (l-i)%3 == 0 {
builder.WriteByte(',')
}
builder.WriteByte(raw[i])
}
// Return the result
return builder.String()
}
// Returns the currency as a human-readable string. Positive numbers are
// prefixed with +.
func (self Currency) DeltaString() string {
s := self.String()
if self.GtZero() {
return "+" + s
}
return s
}
// Addition/division
func (self Currency) Add(num Currency) Currency {
raw := new(big.Int)
raw.Add(self.raw, num.raw)
return Currency{raw}
}
func (self Currency) Sub(num Currency) Currency {
raw := new(big.Int)
raw.Sub(self.raw, num.raw)
return Currency{raw}
}
func (self Currency) Div(num Currency) *big.Float {
return new(big.Float).Quo(self.Float(), num.Float())
}
func (self Currency) Neg() Currency {
return Currency{new(big.Int).Sub(i0, self.raw)}
}
// Comparisons
func (self Currency) Cmp(num Currency) int {
return self.raw.Cmp(num.raw)
}
func (self Currency) Eq(num Currency) bool {
return self.Cmp(num) == 0
}
func (self Currency) Gt(num Currency) bool {
return self.Cmp(num) == 1
}
func (self Currency) Lt(num Currency) bool {
return self.Cmp(num) == -1
}
func (self Currency) LtZero() bool {
return self.raw.Cmp(i0) == -1
}
func (self Currency) IsZero() bool {
return self.raw.Cmp(i0) == 0
}
func (self Currency) GtZero() bool {
return self.raw.Cmp(i0) == 1
}
func (self Currency) IsNil() bool {
return self.raw == nil
}
// Conversions
var f100 *big.Float = big.NewFloat(100)
func (self Currency) Float() *big.Float {
raw := new(big.Float).SetInt(self.raw)
return new(big.Float).Quo(raw, f100)
}
func (self Currency) Int() *big.Int {
return new(big.Int).Set(self.raw)
}
// JSON
func (self Currency) MarshalJSON() ([]byte, error) {
res := []byte(self.RawString())
// Remove a single trailing zero (if any). If all trailing zeroes were
// removed, Python would interpret the value as an integer instead.
if res[len(res)-1] == '0' {
res = res[:len(res)-1]
}
return res, nil
}
func (self *Currency) setString(data string) bool {
if self.raw != nil {
return false
}
if strings.HasPrefix(data, SYMBOL) {
data = data[2:]
}
f, success := new(big.Float).SetString(data)
if success {
res := new(big.Int)
new(big.Float).Mul(f, f100).Int(res)
self.raw = res
}
return success
}
func (self *Currency) UnmarshalJSON(data []byte) error {
// Accept quoted values
var s string
if data[0] == '"' && data[len(data)-1] == '"' {
s = string(data[1 : len(data)-1])
} else {
s = string(data)
}
if self.setString(s) {
return nil
} else {
return errors.New("Invalid currency value.")
}
}
func (self *Currency) GobEncode() ([]byte, error) {
return self.raw.GobEncode()
}
func (self *Currency) GobDecode(data []byte) error {
if self.raw != nil {
return errors.New("GobDecode() on already initialised Currency.")
}
self.raw = new(big.Int)
return self.raw.GobDecode(data)
}
// Create new currency values
func CurrencyFromFloat(num *big.Float) Currency {
f := new(big.Float)
f.Mul(num, f100)
raw := new(big.Int)
f.Int(raw)
return Currency{raw}
}
func CurrencyFromInt(num *big.Int) Currency {
return Currency{new(big.Int).Set(num)}
}
func CurrencyFromInt64(num int64) Currency {
return Currency{new(big.Int).SetInt64(num * 100)}
}
func CurrencyFromFloat64(num float64) Currency {
return CurrencyFromFloat(new(big.Float).SetFloat64(num))
}
func CurrencyFromString(num string) Currency {
var res Currency
if res.setString(strings.Replace(num, "_", "", -1)) {
return res
} else {
return Currency{i0}
}
}
func ParseCurrency(num string) (Currency, error) {
var res Currency
if res.setString(strings.Replace(num, "_", "", -1)) {
return res, nil
} else {
return Currency{i0}, errors.New("ERR_INVALIDAMOUNT")
}
}