-
Notifications
You must be signed in to change notification settings - Fork 150
/
formatter.go
74 lines (61 loc) · 1.55 KB
/
formatter.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
package money
import (
"math"
"strconv"
"strings"
)
// Formatter stores Money formatting information.
type Formatter struct {
Fraction int
Decimal string
Thousand string
Grapheme string
Template string
}
// NewFormatter creates new Formatter instance.
func NewFormatter(fraction int, decimal, thousand, grapheme, template string) *Formatter {
return &Formatter{
Fraction: fraction,
Decimal: decimal,
Thousand: thousand,
Grapheme: grapheme,
Template: template,
}
}
// Format returns string of formatted integer using given currency template.
func (f *Formatter) Format(amount int64) string {
// Work with absolute amount value
sa := strconv.FormatInt(f.abs(amount), 10)
if len(sa) <= f.Fraction {
sa = strings.Repeat("0", f.Fraction-len(sa)+1) + sa
}
if f.Thousand != "" {
for i := len(sa) - f.Fraction - 3; i > 0; i -= 3 {
sa = sa[:i] + f.Thousand + sa[i:]
}
}
if f.Fraction > 0 {
sa = sa[:len(sa)-f.Fraction] + f.Decimal + sa[len(sa)-f.Fraction:]
}
sa = strings.Replace(f.Template, "1", sa, 1)
sa = strings.Replace(sa, "$", f.Grapheme, 1)
// Add minus sign for negative amount.
if amount < 0 {
sa = "-" + sa
}
return sa
}
// ToMajorUnits returns float64 representing the value in sub units using the currency data
func (f *Formatter) ToMajorUnits(amount int64) float64 {
if f.Fraction == 0 {
return float64(amount)
}
return float64(amount) / float64(math.Pow10(f.Fraction))
}
// abs return absolute value of given integer.
func (f Formatter) abs(amount int64) int64 {
if amount < 0 {
return -amount
}
return amount
}