-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfloat.go
91 lines (77 loc) · 1.79 KB
/
float.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
package utils
import (
"bytes"
"math"
"math/big"
"strconv"
"strings"
)
// Round 四舍五入, ROUND_HALF_UP 模式实现
// 返回将 val 根据指定精度 precision (十进制小数点后数字的数目) 进行四舍五入的结果
// precision 也可以是负数或零
// Ref: thinkeridea/go-extend
func Round(v float64, precision int) float64 {
if precision == 0 {
return math.Round(v)
}
p := math.Pow10(precision)
if precision < 0 {
return math.Floor(v*p+0.5) * math.Pow10(-precision)
}
return math.Floor(v*p+0.5) / p
}
// Commaf 浮点数转千分位分隔字符串
// Ref: dustin/go-humanize
// e.g. Commaf(834142.32) -> 834,142.32
func Commaf(v float64) string {
buf := &bytes.Buffer{}
if v < 0 {
buf.Write([]byte{'-'})
v = 0 - v
}
comma := []byte{','}
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)
if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
return buf.String()
}
// BigCommaf big.Float 千分位分隔字符串
// Ref: dustin/go-humanize
func BigCommaf(v *big.Float) string {
buf := &bytes.Buffer{}
if v.Sign() < 0 {
buf.Write([]byte{'-'})
v.Abs(v)
}
comma := []byte{','}
parts := strings.Split(v.Text('f', -1), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)
if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
return buf.String()
}