This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumbers.go
79 lines (72 loc) · 1.82 KB
/
numbers.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
package i18n
import (
"bytes"
"strconv"
"github.com/volatile/core"
)
// Fmtn returns a formatted number with decimal and thousands marks, according to the locale's "decimalMark" and "thousandsMark" keys respectively.
// If not set, the decimal mark is "," and the thousands mark is ".".
func Fmtn(c *core.Context, n interface{}) (s string) {
var b []byte
switch n.(type) {
case uint:
b = []byte(strconv.FormatUint(uint64(n.(uint)), 10))
case uint8:
b = []byte(strconv.FormatUint(uint64(n.(uint8)), 10))
case uint16:
b = []byte(strconv.FormatUint(uint64(n.(uint16)), 10))
case uint32:
b = []byte(strconv.FormatUint(uint64(n.(uint32)), 10))
case uint64:
b = []byte(strconv.FormatUint(n.(uint64), 10))
case int:
b = []byte(strconv.Itoa(n.(int)))
case int8:
b = []byte(strconv.FormatInt(int64(n.(int8)), 10))
case int16:
b = []byte(strconv.FormatInt(int64(n.(int16)), 10))
case int32:
b = []byte(strconv.FormatInt(int64(n.(int32)), 10))
case int64:
b = []byte(strconv.FormatInt(n.(int64), 10))
case float32:
b = []byte(strconv.FormatFloat(float64(n.(float32)), 'f', -1, 32))
case float64:
b = []byte(strconv.FormatFloat(n.(float64), 'f', -1, 64))
case string:
b = []byte(n.(string))
case []byte:
b = n.([]byte)
default:
return
}
decimalMark := "."
thousandsMark := ","
if trs, ok := locales[ClientLocale(c)]; ok {
if v, ok := trs["decimalMark"]; ok {
decimalMark = v
}
if v, ok := trs["thousandsMark"]; ok {
thousandsMark = v
}
}
bb := bytes.Split(b, []byte("."))
switch len(bb) {
case 1:
break
case 2:
s = decimalMark + string(bb[1])
b = bb[0]
default:
return string(b) // Can't have 2 decimal marks in a number so return as provided.
}
j := 0
for i := len(b) - 1; i >= 0; i-- {
if j != 0 && j%3 == 0 {
s = thousandsMark + s
}
s = string(b[i]) + s
j++
}
return
}