-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.go
235 lines (198 loc) · 4.16 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package numbers
import (
"fmt"
"math"
"strconv"
"strings"
)
func Scale(num float64, digits int) float64 {
if num == 0.0 {
return 0.0
}
if math.IsNaN(num) {
return 0.0
}
if math.IsInf(num, 0) {
return num
}
original := num
num = math.Abs(num)
d := math.Ceil(math.Log10(num))
power := digits - int(d)
magnitude := math.Pow10(power)
shifted := math.Ceil(num * magnitude)
result := shifted / magnitude
result = math.Copysign(result, original)
return result
}
func SlideScale(num float64) float64 {
if math.Abs(num) < 100.0 {
return Scale(num, 1)
}
return Scale(num, 2)
}
func ScaleDown(num float64, digits int) float64 {
if num == 0.0 {
return 0.0
}
if math.IsNaN(num) {
return 0.0
}
if math.IsInf(num, 0) {
return num
}
original := num
num = math.Abs(num)
result := 0.0
if num < 1.0 {
result = math.Floor(num*10.0) / 10.0
} else {
d := math.Ceil(math.Log10(num))
power := digits - int(d)
magnitude := math.Pow10(power)
shifted := math.Floor(num * magnitude)
result = shifted / magnitude
}
result = math.Copysign(result, original)
return result
}
func SlideScaleDown(num float64) float64 {
if math.Abs(num) < 100.0 {
return ScaleDown(num, 1)
}
return ScaleDown(num, 2)
}
func CentsToDollars(cents float64) string {
dollars := cents / 100.0
return fmt.Sprintf("$%.2f", dollars)
}
func AddDelimiters(num float64) string {
s := strconv.FormatFloat(num, 'f', 3, 64)
if num < 1000.0 {
return s
}
pieces := strings.Split(s, ".")
digits := []string(nil)
inum := pieces[0]
x := len(inum)
for x > 2 {
digits = append(digits, inum[x-3:x])
x -= 3
}
if x > 0 {
digits = append(digits, inum[0:x])
}
for i, j := 0, len(digits)-1; i < j; i, j = i+1, j-1 {
digits[i], digits[j] = digits[j], digits[i]
}
// drop the fraction
// result := strings.Join(digits, ",") + "." + pieces[1]
result := strings.Join(digits, ",")
return result
}
func AddDelimitersInt(num int) string {
inum := strconv.Itoa(num)
digits := []string(nil)
x := len(inum)
for x > 2 {
digits = append(digits, inum[x-3:x])
x -= 3
}
if x > 0 {
digits = append(digits, inum[0:x])
}
for i, j := 0, len(digits)-1; i < j; i, j = i+1, j-1 {
digits[i], digits[j] = digits[j], digits[i]
}
// drop the fraction
// result := strings.Join(digits, ",") + "." + pieces[1]
result := strings.Join(digits, ",")
return result
}
func Display(num float64) string {
if num >= 10000.0 {
return Humanize(num)
}
if num >= 1000.0 {
return AddDelimiters(num)
}
if num > 1.0 && math.Floor(num) == num {
return fmt.Sprintf("%.0f", num)
}
if num == 0.0 {
return "0"
}
return fmt.Sprintf("%.3f", num)
}
func Humanize(num float64) string {
var exponent = 0.0
if num != 0 {
exponent = math.Floor(math.Log10(math.Abs(num)))
}
if exponent >= 3 {
unit := "K"
dispExponent := 3
if exponent >= 15 {
unit = "Q"
dispExponent = 15
} else if exponent >= 12 {
unit = "T"
dispExponent = 12
} else if exponent >= 9 {
unit = "G"
dispExponent = 9
} else if exponent >= 6 {
unit = "M"
dispExponent = 6
}
num /= math.Pow10(dispExponent)
return fmt.Sprintf("%.2f%s", num, unit)
}
return AddDelimiters(num)
}
func Words(num float64) string {
var exponent = 0.0
if num != 0 {
exponent = math.Floor(math.Log10(math.Abs(num)))
}
if exponent >= 3 {
unit := "thousand"
dispExponent := 3
if exponent >= 15 {
unit = "quadrillion"
dispExponent = 15
} else if exponent >= 12 {
unit = "trillion"
dispExponent = 12
} else if exponent >= 9 {
unit = "billion"
dispExponent = 9
} else if exponent >= 6 {
unit = "million"
dispExponent = 6
}
num /= math.Pow10(dispExponent)
return fmt.Sprintf("%.2f %s", num, unit)
}
return AddDelimiters(num)
}
func Percentage(current, old float64) float64 {
if old == 0.0 {
return 0.0
}
return 100.0 * (current - old) / old
}
func DisplayPercentage(percentage float64) string {
return Display(percentage)
}
// using midpoint method
func PercentageMid(current, old float64) float64 {
mid := 0.5 * (current + old)
if mid == 0.0 {
return 0.0
}
return 100.0 * (current - old) / mid
}
func Megabytes(bytes uint64) float64 {
return float64(bytes) / (1024.0 * 1024.0)
}