fermi/pretty/pretty.go

52 lines
1.1 KiB
Go
Raw Normal View History

2024-06-19 02:44:24 +00:00
package pretty
import (
"fmt"
"math"
)
func PrettyPrintInt(n int) {
switch {
case math.Abs(float64(n)) >= 1_000_000_000_000:
fmt.Printf("%dT", n/1_000_000_000_000)
case math.Abs(float64(n)) >= 1_000_000_000:
fmt.Printf("%dB", n/1_000_000_000)
case math.Abs(float64(n)) >= 1_000_000:
fmt.Printf("%dM", n/1_000_000)
case math.Abs(float64(n)) >= 1_000:
fmt.Printf("%dK", n/1_000)
default:
fmt.Printf("%df", n)
}
}
func PrettyPrintFloat(f float64) {
switch {
case math.Abs(f) >= 1_000_000_000_000:
fmt.Printf("%.1fT", f/1_000_000_000_000)
case math.Abs(f) >= 1_000_000_000:
fmt.Printf("%.1fB", f/1_000_000_000)
case math.Abs(f) >= 1_000_000:
fmt.Printf("%.1fM", f/1_000_000)
case math.Abs(f) >= 1_000:
fmt.Printf("%.1fK", f/1_000)
case math.Abs(f) <= 0.0001:
fmt.Printf("%.5f", f)
case math.Abs(f) <= 0.001:
fmt.Printf("%.4f", f)
case math.Abs(f) <= 0.01:
fmt.Printf("%.3f", f)
case math.Abs(f) <= 0.1:
fmt.Printf("%.2f", f)
default:
fmt.Printf("%.1f", f)
}
}
func PrettyPrint2Floats(low float64, high float64) {
PrettyPrintFloat(low)
fmt.Printf(" ")
PrettyPrintFloat(high)
}