-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
73 lines (67 loc) · 1.87 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
package money
import (
"fmt"
"math"
"strconv"
"strings"
)
// Money is amount of money as integer number of minor units and currency code.
// E.g.: one dollar is Money{100, "USD"} cause "USD" currency has scale 2.
type Money struct {
Amount int64
Currency string
}
// Make creates Money by normalizing amount/scale according to currencies system.
// E.g. Money(100, 0, "USD") gives Money{100_00, "USD"}.
// Rounding is done via math.Round.
func Make(amount int64, scale int, currencyCode string) Money {
targetScale := ScaleForCurrency(currencyCode)
if targetScale == scale {
return Money{
Amount: amount,
Currency: currencyCode,
}
}
amount = int64(math.Round(float64(amount) * math.Pow10(targetScale-scale)))
return Money{
Amount: amount,
Currency: currencyCode,
}
}
// FromFloat create Money instance rounding float value according to currency scale.
func FromFloat(val float64, currencyCode string) Money {
scale := ScaleForCurrency(currencyCode)
amount := int64(math.Round(val * math.Pow10(scale)))
return Money{
Amount: amount,
Currency: currencyCode,
}
}
// String creates string representation, e.g. "100.00 USD" or "100 KRW" (currency with scale 0).
func (m Money) String() string {
var fmtStr string
scale := ScaleForCurrency(m.Currency)
if scale > 0 {
fmtStr = "%." + strconv.Itoa(scale) + "f"
} else {
fmtStr = "%.0f"
}
s := fmt.Sprintf(fmtStr, float64(m.Amount)/math.Pow10(scale))
if m.Currency == "" {
return s
}
return s + " " + m.Currency
}
// FromString parses Money from string that has two parts: float value and currency code.
func FromString(s string) (Money, error) {
parts := strings.SplitN(s, " ", 2)
val, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return Money{}, fmt.Errorf("parsing amount: %s", err)
}
var currency string
if len(parts) > 1 {
currency = parts[1]
}
return FromFloat(val, currency), nil
}