2024-06-19 02:44:24 +00:00
|
|
|
package pretty
|
|
|
|
|
|
|
|
import (
|
2024-06-30 13:19:20 +00:00
|
|
|
"errors"
|
2024-06-19 02:44:24 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2024-06-30 13:19:20 +00:00
|
|
|
"strconv"
|
2024-06-19 02:44:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func PrettyPrintInt(n int) {
|
|
|
|
switch {
|
|
|
|
case math.Abs(float64(n)) >= 1_000_000_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fT", float64(n)/1_000_000_000_000.0)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(float64(n)) >= 1_000_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fB", float64(n)/1_000_000_000.0)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(float64(n)) >= 1_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fM", float64(n)/1_000_000.0)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(float64(n)) >= 1_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fK", float64(n)/1_000.0)
|
2024-06-19 02:44:24 +00:00
|
|
|
default:
|
2024-08-12 16:34:28 +00:00
|
|
|
fmt.Printf("%d", n)
|
2024-06-19 02:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrettyPrintFloat(f float64) {
|
|
|
|
switch {
|
|
|
|
case math.Abs(f) >= 1_000_000_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fT", f/1_000_000_000_000)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) >= 1_000_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fB", f/1_000_000_000)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) >= 1_000_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fM", f/1_000_000)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) >= 1_000:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2fK", f/1_000)
|
2024-06-19 02:44:24 +00:00
|
|
|
|
|
|
|
case math.Abs(f) <= 0.0001:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.6f", f)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) <= 0.001:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.5f", f)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) <= 0.01:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.4f", f)
|
2024-06-19 02:44:24 +00:00
|
|
|
case math.Abs(f) <= 0.1:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.3f", f)
|
2024-06-19 02:44:24 +00:00
|
|
|
default:
|
2024-08-12 16:27:33 +00:00
|
|
|
fmt.Printf("%.2f", f)
|
2024-06-19 02:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func PrettyPrint2Floats(low float64, high float64) {
|
|
|
|
PrettyPrintFloat(low)
|
|
|
|
fmt.Printf(" ")
|
|
|
|
PrettyPrintFloat(high)
|
|
|
|
}
|
2024-06-30 13:19:20 +00:00
|
|
|
|
2024-06-30 13:30:55 +00:00
|
|
|
func ParseFloat(word string) (float64, error) {
|
2024-06-30 13:19:20 +00:00
|
|
|
// l = len(word) // assuming no UTF stuff
|
2024-06-30 13:22:04 +00:00
|
|
|
switch len(word) {
|
|
|
|
case 0:
|
2024-06-30 13:19:20 +00:00
|
|
|
return 0, errors.New("String to be parsed into float must not be the empty string")
|
2024-06-30 13:22:04 +00:00
|
|
|
case 1:
|
|
|
|
return strconv.ParseFloat(word, 64)
|
2024-06-30 13:19:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 13:22:04 +00:00
|
|
|
n := len(word) - 1
|
|
|
|
f, err := strconv.ParseFloat(word[:n], 64)
|
2024-11-20 11:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
multiplier := 1.0
|
2024-06-30 13:19:20 +00:00
|
|
|
switch word[n] {
|
2024-11-10 17:13:01 +00:00
|
|
|
case '%':
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 0.01
|
2024-06-30 13:22:04 +00:00
|
|
|
case 'K':
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 1_000
|
2024-06-30 13:22:04 +00:00
|
|
|
case 'M':
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 1_000_000
|
2024-06-30 13:22:04 +00:00
|
|
|
case 'B':
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 1_000_000
|
2024-06-30 13:22:04 +00:00
|
|
|
case 'T':
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 1_000_000
|
2024-06-30 13:19:20 +00:00
|
|
|
default:
|
2024-11-20 11:19:10 +00:00
|
|
|
multiplier = 1.0
|
2024-06-30 13:19:20 +00:00
|
|
|
}
|
2024-11-20 11:19:10 +00:00
|
|
|
return f * multiplier, nil
|
2024-06-30 13:19:20 +00:00
|
|
|
}
|