forked from govalues/money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange_rate.go
408 lines (365 loc) · 11.1 KB
/
exchange_rate.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package money
import (
"fmt"
"github.com/govalues/decimal"
)
// ExchangeRate represents a unidirectional exchange rate between two currencies.
// The zero value corresponds to an exchange rate of "XXX/XXX 0", where XXX indicates
// an unknown currency.
// This type is designed to be safe for concurrent use by multiple goroutines.
type ExchangeRate struct {
base Currency // currency being exchanged
quote Currency // currency being obtained in exchange for the base currency
value decimal.Decimal // how many units of quote currency are needed to exchange for 1 unit of the base currency
}
func newExchRateUnsafe(base, quote Currency, rate decimal.Decimal) ExchangeRate {
return ExchangeRate{base: base, quote: quote, value: rate}
}
// NewExchRate returns a new exchange rate between the base and quote currencies.
// If the scale of the rate is less than the sum of the scales of its base and
// quote currencies, the result will be zero-padded to the right.
func NewExchRate(base, quote Currency, rate decimal.Decimal) (ExchangeRate, error) {
if !rate.IsPos() {
return ExchangeRate{}, fmt.Errorf("exchange rate must be positive")
}
if base == quote && !rate.IsOne() {
return ExchangeRate{}, fmt.Errorf("exchange rate must be equal to 1")
}
if rate.Scale() < base.Scale()+quote.Scale() {
var err error
rate, err = rate.Rescale(base.Scale() + quote.Scale())
if err != nil {
return ExchangeRate{}, fmt.Errorf("exchange rate rescaling: %w", err)
}
}
return newExchRateUnsafe(base, quote, rate), nil
}
// ParseExchRate converts currency and decimal strings to a (possibly rounded) exchange rate.
// If the scale of the rate is less than the sum of the scales of its base and
// quote currencies, the result will be zero-padded to the right.
// See also methods [ParseCurr] and [decimal.Parse].
func ParseExchRate(base, quote, rate string) (ExchangeRate, error) {
b, err := ParseCurr(base)
if err != nil {
return ExchangeRate{}, fmt.Errorf("base currency parsing: %w", err)
}
q, err := ParseCurr(quote)
if err != nil {
return ExchangeRate{}, fmt.Errorf("quote currency parsing: %w", err)
}
d, err := decimal.ParseExact(rate, b.Scale()+q.Scale())
if err != nil {
return ExchangeRate{}, fmt.Errorf("rate parsing: %w", err)
}
r, err := NewExchRate(b, q, d)
if err != nil {
return ExchangeRate{}, fmt.Errorf("rate construction: %w", err)
}
return r, nil
}
// MustParseExchRate is like [ParseExchRate] but panics if any of the strings cannot be parsed.
// It simplifies safe initialization of global variables holding exchange rates.
func MustParseExchRate(base, quote, rate string) ExchangeRate {
r, err := ParseExchRate(base, quote, rate)
if err != nil {
panic(fmt.Sprintf("ParseExchRate(%q, %q, %q) failed: %v", base, quote, rate, err))
}
return r
}
// Base returns the currency being exchanged.
func (r ExchangeRate) Base() Currency {
return r.base
}
// Quote returns the currency being obtained in exchange for the base currency.
func (r ExchangeRate) Quote() Currency {
return r.quote
}
// Mul returns an exchange rate with the same base and quote currencies,
// but with the rate multiplied by a positive factor e.
//
// Mul returns an error if factor e is not positive.
func (r ExchangeRate) Mul(e decimal.Decimal) (ExchangeRate, error) {
if !e.IsPos() {
return ExchangeRate{}, fmt.Errorf("negative factor")
}
d := r.value
f, err := d.MulExact(e, r.Base().Scale()+r.Quote().Scale())
if err != nil {
return ExchangeRate{}, fmt.Errorf("exchange rate multiplication: %w", err)
}
return NewExchRate(r.Base(), r.Quote(), f)
}
// CanConv returns true if [ExchangeRate.Conv] can be used to convert the given amount.
func (r ExchangeRate) CanConv(b Amount) bool {
return b.Curr() == r.Base() &&
r.Base() != XXX &&
r.Quote() != XXX &&
r.value.IsPos()
}
// Conv returns the amount converted from the base currency to the quote currency.
//
// Conv returns an error if the base currency of the exchange rate does not match
// the currency of the given amount.
func (r ExchangeRate) Conv(b Amount) (Amount, error) {
if !r.CanConv(b) {
return Amount{}, errCurrencyMismatch
}
d, e := r.value, b.value
f, err := d.MulExact(e, r.Quote().Scale())
if err != nil {
return Amount{}, fmt.Errorf("amount conversion: %w", err)
}
return NewAmount(r.Quote(), f)
}
// Inv returns the inverse of the exchange rate.
func (r ExchangeRate) Inv() (ExchangeRate, error) {
d, e := decimal.One, r.value
f, err := d.Quo(e)
if err != nil {
return ExchangeRate{}, fmt.Errorf("exchange rate inversion: %w", err)
}
return NewExchRate(r.Quote(), r.Base(), f)
}
// SameCurr returns true if exchange rates are denominated in the same base
// and quote currencies.
// See also methods [ExchangeRate.Base] and [ExchangeRate.Quote].
func (r ExchangeRate) SameCurr(q ExchangeRate) bool {
return q.Base() == r.Base() && q.Quote() == r.Quote()
}
// SameScale returns true if exchange rates have the same scale.
// See also method [ExchangeRate.Scale].
func (r ExchangeRate) SameScale(q ExchangeRate) bool {
return q.Scale() == r.Scale()
}
// SameScaleAsCurr returns true if the scale of the exchange rate is equal to
// the sum of the scales of its base and quote currencies.
// See also method [ExchangeRate.RoundToCurr].
func (r ExchangeRate) SameScaleAsCurr() bool {
return r.Scale() == r.Base().Scale()+r.Quote().Scale()
}
// Prec returns the number of digits in the coefficient.
func (r ExchangeRate) Prec() int {
return r.value.Prec()
}
// Scale returns the number of digits after the decimal point.
func (r ExchangeRate) Scale() int {
return r.value.Scale()
}
// IsZero returns:
//
// true if r == 0
// false otherwise
func (r ExchangeRate) IsZero() bool {
return r.value.IsZero()
}
// IsOne returns:
//
// true if r == 1
// false otherwise
func (r ExchangeRate) IsOne() bool {
return r.value.IsOne()
}
// WithinOne returns:
//
// true if 0 <= r < 1
// false otherwise
func (r ExchangeRate) WithinOne() bool {
return r.value.WithinOne()
}
// Round returns an exchange rate that is rounded to the given number of digits
// after the decimal point.
// If the specified scale is less than the sum of the scales of the base and quote
// currency then the exchange rate will be rounded to the sum of scales instead.
// See also method [ExchangeRate.RoundToCurr].
func (r ExchangeRate) Round(scale int) ExchangeRate {
if scale < r.Base().Scale()+r.Quote().Scale() {
scale = r.Base().Scale() + r.Quote().Scale()
}
d := r.value
return newExchRateUnsafe(r.Base(), r.Quote(), d.Round(scale))
}
// RoundToCurr returns an exchange rate that is rounded to the sum of the scales of its base
// and quote currencies.
// See also method [ExchangeRate.SameScaleAsCurr].
func (r ExchangeRate) RoundToCurr() ExchangeRate {
return r.Round(r.Base().Scale() + r.Quote().Scale())
}
// Rescale returns an exchange rate that is rounded or zero-padded to the given
// number of digits after the decimal point.
// If the specified scale is less than the sum of the scales of the base and quote
// currency then the exchange rate will be rounded to the sum of scales instead.
//
// Rescale returns an error if the integer part of the result exceeds the maximum
// precision, calculated as ([decimal.MaxPrec] - scale).
func (r ExchangeRate) Rescale(scale int) (ExchangeRate, error) {
if scale < r.Base().Scale()+r.Quote().Scale() {
scale = r.Base().Scale() + r.Quote().Scale()
}
d := r.value
d, err := d.Rescale(scale)
if err != nil {
return ExchangeRate{}, fmt.Errorf("rescaling %q to %v decimal places: %w", r, scale, err)
}
return NewExchRate(r.Base(), r.Quote(), d)
}
// String method implements the [fmt.Stringer] interface and returns a string
// representation of the exchange rate.
// See also methods [Currency.String] and [Decimal.String].
//
// [fmt.Stringer]: https://pkg.go.dev/fmt#Stringer
// [Decimal.String]: https://pkg.go.dev/github.com/govalues/decimal#Decimal.String
func (r ExchangeRate) String() string {
return r.Base().String() + "/" + r.Quote().String() + " " + r.value.String()
}
// Format implements the [fmt.Formatter] interface.
// The following [format verbs] are available:
//
// %s, %v: USD/EUR 1.2345
// %q: "USD/EUR 1.2345"
// %f: 1.2345
// %c: USD/EUR
//
// The '-' format flag can be used with all verbs.
// The '0' format flags can be used with all verbs except %c.
//
// Precision is only supported for the %f verb.
// The default precision is equal to the sum of the scales of its base and quote currencies.
//
// [format verbs]: https://pkg.go.dev/fmt#hdr-Printing
// [fmt.Formatter]: https://pkg.go.dev/fmt#Formatter
//
//gocyclo:ignore
func (r ExchangeRate) Format(state fmt.State, verb rune) {
// Rescaling
tzeroes := 0
if verb == 'f' || verb == 'F' {
scale := 0
switch p, ok := state.Precision(); {
case ok:
scale = p
default:
scale = r.Base().Scale() + r.Quote().Scale()
}
switch {
case scale < r.Scale():
r = r.Round(scale)
case scale > r.Scale():
tzeroes = scale - r.Scale()
}
}
// Integer and fractional digits
intdigs, fracdigs := 0, 0
switch rprec := r.Prec(); verb {
case 'c', 'C':
// skip
default:
fracdigs = r.Scale()
if rprec > fracdigs {
intdigs = rprec - fracdigs
}
if r.WithinOne() {
intdigs++ // leading 0
}
}
// Decimal point
dpoint := 0
if fracdigs > 0 || tzeroes > 0 {
dpoint = 1
}
// Currency symbols
curr := ""
switch verb {
case 'f', 'F':
// skip
case 'c', 'C':
curr = r.Base().String() + "/" + r.Quote().String()
default:
curr = r.Base().String() + "/" + r.Quote().String() + " "
}
currlen := len(curr)
// Opening and closing quotes
lquote, tquote := 0, 0
if verb == 'q' || verb == 'Q' {
lquote, tquote = 1, 1
}
// Calculating padding
width := lquote + intdigs + dpoint + fracdigs + tzeroes + currlen + tquote
lspaces, lzeroes, tspaces := 0, 0, 0
if w, ok := state.Width(); ok && w > width {
switch {
case state.Flag('-'):
tspaces = w - width
case state.Flag('0') && verb != 'c' && verb != 'C':
lzeroes = w - width
default:
lspaces = w - width
}
width = w
}
buf := make([]byte, width)
pos := width - 1
// Trailing spaces
for i := 0; i < tspaces; i++ {
buf[pos] = ' '
pos--
}
// Closing quote
if tquote > 0 {
buf[pos] = '"'
pos--
}
// Trailing zeroes
for i := 0; i < tzeroes; i++ {
buf[pos] = '0'
pos--
}
// Fractional digits
coef := r.value.Coef()
for i := 0; i < fracdigs; i++ {
buf[pos] = byte(coef%10) + '0'
pos--
coef /= 10
}
// Decimal point
if dpoint > 0 {
buf[pos] = '.'
pos--
}
// Integer digits
for i := 0; i < intdigs; i++ {
buf[pos] = byte(coef%10) + '0'
pos--
coef /= 10
}
// Leading zeroes
for i := 0; i < lzeroes; i++ {
buf[pos] = '0'
pos--
}
// Currency symbols
for i := currlen; i > 0; i-- {
buf[pos] = curr[i-1]
pos--
}
// Opening quote
if lquote > 0 {
buf[pos] = '"'
pos--
}
// Leading spaces
for i := 0; i < lspaces; i++ {
buf[pos] = ' '
pos--
}
// Writing result
switch verb {
case 'q', 'Q', 's', 'S', 'v', 'V', 'f', 'F', 'c', 'C':
state.Write(buf)
default:
state.Write([]byte("%!"))
state.Write([]byte{byte(verb)})
state.Write([]byte("(money.ExchangeRate="))
state.Write(buf)
state.Write([]byte(")"))
}
}