convert.go114 lines, 3.6 KB
| 1 | package fx |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "math/big" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | // exponents is how many digits each currency has after the decimal point, from |
| 12 | // ISO 4217: a dollar is 100 cents, a yen has no minor unit, and a Kuwaiti dinar |
| 13 | // is 1,000 fils. |
| 14 | var exponents = map[string]int{ |
| 15 | "EUR": 2, |
| 16 | "GBP": 2, |
| 17 | "JPY": 0, |
| 18 | "KWD": 3, |
| 19 | "USD": 2, |
| 20 | } |
| 21 | |
| 22 | // Rounding says which way an amount goes when it falls between two minor units. |
| 23 | type Rounding int |
| 24 | |
| 25 | const ( |
| 26 | // Down is for what we pay out: never a fraction of a cent more than the rate gives. |
| 27 | Down Rounding = iota |
| 28 | // Up is for what we charge: never a fraction of a cent less than the rate asks. |
| 29 | Up |
| 30 | ) |
| 31 | |
| 32 | // ErrTooLarge is a conversion whose result doesn't fit in an int64. |
| 33 | var ErrTooLarge = errors.New("fx: amount too large") |
| 34 | |
| 35 | // Convert returns amount, in minor units of from, in minor units of to, at |
| 36 | // rate, rounded r. The rate is per major unit, so the exponents move the |
| 37 | // decimal point: 1,000 yen at 0.0066063 is 6.6063 dollars, 660 cents, rounded down. |
| 38 | func Convert(amount int64, from, to string, rate Rate, r Rounding) (int64, error) { |
| 39 | num, den, err := factor(from, to, rate) |
| 40 | if err != nil { |
| 41 | return 0, err |
| 42 | } |
| 43 | return mulDiv(amount, num, den, r) |
| 44 | } |
| 45 | |
| 46 | // Cost is Convert backwards: the minor units of from that buy amount minor |
| 47 | // units of to, at rate. It rounds up, to the smallest amount that Convert, |
| 48 | // rounding down, turns into amount or more. |
| 49 | func Cost(amount int64, from, to string, rate Rate) (int64, error) { |
| 50 | num, den, err := factor(from, to, rate) |
| 51 | if err != nil { |
| 52 | return 0, err |
| 53 | } |
| 54 | return mulDiv(amount, den, num, Up) |
| 55 | } |
| 56 | |
| 57 | // factor returns the fraction that turns minor units of from into minor units |
| 58 | // of to: rate × 10^(to's exponent) ÷ 10^(RateDecimals + from's exponent). |
| 59 | func factor(from, to string, rate Rate) (num, den *big.Int, err error) { |
| 60 | ef, ok := exponents[from] |
| 61 | et, ok2 := exponents[to] |
| 62 | if !ok || !ok2 || rate <= 0 { |
| 63 | return nil, nil, fmt.Errorf("fx: can't convert %s to %s at %s", from, to, rate) |
| 64 | } |
| 65 | num = new(big.Int).Mul(big.NewInt(int64(rate)), pow10(et)) |
| 66 | return num, pow10(RateDecimals + ef), nil |
| 67 | } |
| 68 | |
| 69 | // mulDiv returns amount × num ÷ den, rounded r. It works in math/big: a |
| 70 | // million dollars in cents, times a rate in ten-billionths, is already |
| 71 | // bigger than the largest int64, and an int64 overflows without a word. |
| 72 | func mulDiv(amount int64, num, den *big.Int, r Rounding) (int64, error) { |
| 73 | n := new(big.Int).Mul(big.NewInt(amount), num) |
| 74 | q, rem := n.QuoRem(n, den, new(big.Int)) |
| 75 | if r == Up && rem.Sign() > 0 { |
| 76 | q.Add(q, big.NewInt(1)) |
| 77 | } |
| 78 | if !q.IsInt64() { |
| 79 | return 0, ErrTooLarge |
| 80 | } |
| 81 | return q.Int64(), nil |
| 82 | } |
| 83 | |
| 84 | func pow10(n int) *big.Int { |
| 85 | return new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(n)), nil) |
| 86 | } |
| 87 | |
| 88 | // Fee returns bps basis points of amount, a hundredth of a percent each, |
| 89 | // rounded up: 50 basis points of 54.26 dollars is 28 cents, not 27.13. |
| 90 | func Fee(amount, bps int64) int64 { |
| 91 | return (amount*bps + 9_999) / 10_000 |
| 92 | } |
| 93 | |
| 94 | // Format writes amount, in minor units of currency, as a decimal with the |
| 95 | // currency's own number of places: 1500 USD is 15.00, -5 USD is -0.05, 1500 JPY |
| 96 | // is 1500, and 1500 KWD is 1.500. It works on the digits: floats never touch money. |
| 97 | func Format(amount int64, currency string) (string, error) { |
| 98 | exp, ok := exponents[currency] |
| 99 | if !ok { |
| 100 | return "", fmt.Errorf("fx: unknown currency %q", currency) |
| 101 | } |
| 102 | digits, sign := strconv.FormatInt(amount, 10), "" |
| 103 | if amount < 0 { |
| 104 | digits, sign = digits[1:], "-" |
| 105 | } |
| 106 | if exp == 0 { |
| 107 | return sign + digits, nil |
| 108 | } |
| 109 | if len(digits) <= exp { |
| 110 | digits = strings.Repeat("0", exp-len(digits)+1) + digits // 5 cents is 005 |
| 111 | } |
| 112 | cut := len(digits) - exp |
| 113 | return sign + digits[:cut] + "." + digits[cut:], nil |
| 114 | } |