-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.go
322 lines (264 loc) · 6.27 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// currency.go - Atto dollars represented as big.Int
//
// (c) 2017, Sudhi Herle <[email protected]>
//
//
// Licensing Terms: GPLv2
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
// Package currency implements a decimal currency type as
// "atto dollars" (1.0e-18) represented as a big.Int.
// All arithmetic is done on the underlying Big.Int. By default, the
// output conversion to string uses the full (18 decimal digit)
// precision. Output string representation is not rounded - but
// truncated.
package currency
import (
"fmt"
"math/big"
"strings"
)
// A currency is represented as atto dollars (18 digits of precision)
type Currency struct {
big.Int
}
// Atto exponent, multiplication factor and padding string
const eExp = 18
var iMult int64 // multiplicative factor for 'characteristic'
var iBigMult *big.Int // same as iMult, but as a big.Int
var zeroes string // array of zeroes
var zero *big.Int // "zero" currency
func init() {
for i := 0; i < eExp; i++ {
zeroes += "0"
}
iMult = pow64(10, eExp)
iBigMult = big.NewInt(iMult)
zero = big.NewInt(0)
}
// compute a ** b for unsigned quantities using binary
// exponentiation method
func pow64(a int64, b int) int64 {
var r int64 = 1
for b > 0 {
if 0 != (b & 1) {
r *= a
}
b >>= 1
a *= a
}
return r
}
// Create a zero valued currency
func New() *Currency {
return &Currency{}
}
// Make a new Currency instance with input string 's' and output
// precision of 'oprec'. If 'oprec' is more than Atto Dollars, it is
// clamped at Atto Dollars (18). If it is less than or equal to
// zero, it is clamped at 6.
func NewFromString(s string) (*Currency, error) {
p := &Currency{}
if len(s) > 0 {
err := parse(&p.Int, s)
if err != nil {
return nil, err
}
}
return p, nil
}
// Convert 'p' to a string - bounded by output precision
// We just shift 12 digits off the left and print it.
func (p Currency) String() string {
return stringify(&p.Int, eExp)
}
// Show 'p' to a string bounded by output precision 'oprec'
// If 'oprec' is more than the atto-dollar resolution, it is clamped
// at 12.
func (p *Currency) StringFixed(oprec int) string {
if oprec > eExp || oprec <= 0 {
oprec = eExp
}
return stringify(&p.Int, oprec)
}
// stringify atto-dollars in 'b'
func stringify(b *big.Int, oprec int) string {
var m, x string
s := b.String()
// Not enough atto dollars
if len(s) <= eExp {
m = "0"
if lpad := eExp - len(s); lpad > 0 {
x = zeroes[:lpad] + s
} else {
x = s
}
} else if len(s) > eExp {
n := len(s) - eExp
m, x = s[:n], s[n:]
}
if len(x) > oprec {
x = x[:oprec]
}
return fmt.Sprintf("%s.%s", m, x)
}
// Add 'x' to 'p'
func (p *Currency) Add(x *Currency) *Currency {
p.Int.Add(&p.Int, &x.Int)
return p
}
// Subtract 'x' from 'p'
func (p *Currency) Sub(x *Currency) *Currency {
p.Int.Sub(&p.Int, &x.Int)
return p
}
// Multiply 'p' with 'x'
func (p *Currency) Mul(x *Currency) *Currency {
p.Int.Mul(&p.Int, &x.Int)
return p
}
// Divide 'p' by 'x' and return the dividend
func (p *Currency) Div(x *Currency) *Currency {
p.Int.Quo(&p.Int, &x.Int)
return p
}
// Divide 'p' by 'x', and set p to the quotient and return 'p' and
// the remainder. This implements math/big's DivMod (Euclidean
// division):
// q = p div x
// r = p - (x * q)
// p = q
// return p, r
func (p *Currency) DivMod(x *Currency) (*Currency, *Currency) {
var r big.Int
p.Int.DivMod(&p.Int, &x.Int, &r)
return p, &Currency{Int: r}
}
// Return true if this is zero
func (p *Currency) IsZero() bool {
return 0 == p.Cmp(zero)
}
// Return true if 'p' is equal to 'x', false otherwise
func (p *Currency) Eq(x *Currency) bool {
return 0 == p.Int.Cmp(&x.Int)
}
// Return a+b
func Add(a, b *Currency) *Currency {
var z big.Int
z.Add(&a.Int, &b.Int)
return &Currency{Int: z}
}
// Return a-b
func Sub(a, b *Currency) *Currency {
var z big.Int
z.Sub(&a.Int, &b.Int)
return &Currency{Int: z}
}
// Return a*b
func Mul(a, b *Currency) *Currency {
var z big.Int
z.Mul(&a.Int, &b.Int)
return &Currency{Int: z}
}
// Return a/b
func Div(a, b *Currency) *Currency {
var z big.Int
z.Quo(&a.Int, &b.Int)
return &Currency{Int: z}
}
// Do Euclidean division of a by b, return the quotient and
// reminder.
func DivMod(a, b *Currency) (*Currency, *Currency) {
var z big.Int
var r big.Int
z.DivMod(&a.Int, &b.Int, &r)
return &Currency{Int: z}, &Currency{Int: r}
}
// Return 1/a
func Inv(a *Currency) *Currency {
var z big.Int
z.Quo(iBigMult, &a.Int)
return &Currency{Int: z}
}
// Return true of a == b
func Eq(a, b *Currency) bool {
return 0 == a.Int.Cmp(&b.Int)
}
// Return -1, 0, +1 if a < b, a == b, a > b respectively
func Cmp(a, b *Currency) int {
return a.Int.Cmp(&b.Int)
}
// Marshal 'p' to JSON
func (p *Currency) MarshalJSON() ([]byte, error) {
s := p.String()
return []byte(s), nil
}
// Unmarshal JSON to 'p'
func (p *Currency) UnmarshalJSON(txt []byte) error {
return parse(&p.Int, string(txt))
}
// Parse a valid string 's' into a atto-dollar big.Int
func parse(p *big.Int, s string) error {
v := strings.Split(s, ".")
var pre, post string
switch len(v) {
case 1:
pre = zstripPre(s)
case 2:
pre = zstripPre(v[0])
post = zstripPost(v[1])
default:
return fmt.Errorf("malformed decimal %s", s)
}
if len(pre) > 0 {
if _, ok := p.SetString(pre, 10); !ok {
return fmt.Errorf("invalid decimal %s", s)
}
}
// Truncate longer mantissae
if len(post) > eExp {
post = post[:eExp]
}
// We need the length of the string before we strip out the
// leading zeroes. This length tells us how large the exponent of
// the mantissa should be.
n := len(post)
post = zstripPre(post)
f := &big.Int{}
if len(post) > 0 {
if _, ok := f.SetString(post, 10); !ok {
return fmt.Errorf("invalid fraction %s", s)
}
}
if exp := eExp - n; exp > 0 {
var m big.Int
m.SetInt64(pow64(10, exp))
f.Mul(f, &m)
}
p.Mul(p, iBigMult)
p.Add(p, f)
return nil
}
func zstripPre(s string) string {
n := len(s)
for i := 0; i < n; i++ {
if s[i] != '0' {
return s[i:]
}
}
return ""
}
func zstripPost(s string) string {
n := len(s)
for i := n - 1; i >= 0; i-- {
if s[i] != '0' {
return s[:i+1]
}
}
return ""
}