This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathquanity_util.go
executable file
·134 lines (108 loc) · 2.32 KB
/
quanity_util.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package tableprinter
import (
"math"
"strconv"
"strings"
)
var units = [...]string{"K", "M", "B", "T"}
/*
references & credits:
- https://github.com/DeyV/gotools/blob/master/numbers.go
- https://golang.org/pkg/math/#Modf
*/
func nearestThousandFormat(num float64) string {
if math.Abs(num) < 999.5 {
xNum := formatNumber(num)
xNumStr := xNum[:len(xNum)-3]
return xNumStr
}
xNum := formatNumber(num)
xNumStr := xNum[:len(xNum)-3]
xNumCleaned := strings.Replace(xNumStr, ",", " ", -1)
xNumSlice := strings.Fields(xNumCleaned)
count := len(xNumSlice) - 2
xPart := units[count]
afterDecimal := ""
if xNumSlice[1][0] != 0 {
afterDecimal = "." + string(xNumSlice[1][0])
}
final := xNumSlice[0] + afterDecimal + xPart
return final
}
func formatNumber(input float64) string {
x := roundInt(input)
xFormatted := numberFormat(float64(x), 2, ".", ",")
return xFormatted
}
func roundInt(input float64) int {
var result float64
if input < 0 {
result = math.Ceil(input - 0.5)
} else {
result = math.Floor(input + 0.5)
}
i, _ := math.Modf(result)
return int(i)
}
func numberFormat(number float64, decimals int, decPoint, thousandsSep string) string {
if math.IsNaN(number) || math.IsInf(number, 0) {
number = 0
}
var ret string
var negative bool
if number < 0 {
number *= -1
negative = true
}
d, fract := math.Modf(number)
if decimals <= 0 {
fract = 0
} else {
pow := math.Pow(10, float64(decimals))
fract = roundPrec(fract*pow, 0)
}
if thousandsSep == "" {
ret = strconv.FormatFloat(d, 'f', 0, 64)
} else if d >= 1 {
var x float64
for d >= 1 {
d, x = math.Modf(d / 1000)
x = x * 1000
ret = strconv.FormatFloat(x, 'f', 0, 64) + ret
if d >= 1 {
ret = thousandsSep + ret
}
}
} else {
ret = "0"
}
fracts := strconv.FormatFloat(fract, 'f', 0, 64)
for i := len(fracts); i < decimals; i++ {
fracts = "0" + fracts
}
ret += decPoint + fracts
if negative {
ret = "-" + ret
}
return ret
}
func roundPrec(x float64, prec int) float64 {
if math.IsNaN(x) || math.IsInf(x, 0) {
return x
}
sign := 1.0
if x < 0 {
sign = -1
x *= -1
}
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
if frac >= 0.5 {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}
return rounder / pow * sign
}