-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompact_float.go
More file actions
37 lines (36 loc) · 884 Bytes
/
compact_float.go
File metadata and controls
37 lines (36 loc) · 884 Bytes
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
package fmt
// formatCompactFloat mimics Go's %g/%G: uses %f for normal range, %e/%E for very small/large, trims trailing zeros.
func formatCompactFloat(f float64, precision int, upper bool) string {
if precision < 0 {
precision = 6
}
absf := f
if absf < 0 {
absf = -absf
}
// Use scientific for very small or large numbers
if absf != 0 && (absf < 1e-4 || absf >= 1e6) {
return formatScientific(f, precision, upper)
}
// Use %f, then trim trailing zeros and dot
mult := 1.0
for i := 0; i < precision; i++ {
mult *= 10
}
val := int64(f*mult + 0.5)
intPart := val / int64(mult)
fracPart := val % int64(mult)
res := itoa(int(intPart))
if precision > 0 {
frac := itoaPad(int(fracPart), precision)
// TrimSpace trailing zeros
end := len(frac)
for end > 0 && frac[end-1] == '0' {
end--
}
if end > 0 {
res += "." + frac[:end]
}
}
return res
}