package pretty import ( "errors" "fmt" "math" "strconv" ) func PrettyPrintInt(n int) { switch { case math.Abs(float64(n)) >= 1_000_000_000_000: fmt.Printf("%.2fT", float64(n)/1_000_000_000_000.0) case math.Abs(float64(n)) >= 1_000_000_000: fmt.Printf("%.2fB", float64(n)/1_000_000_000.0) case math.Abs(float64(n)) >= 1_000_000: fmt.Printf("%.2fM", float64(n)/1_000_000.0) case math.Abs(float64(n)) >= 1_000: fmt.Printf("%.2fK", float64(n)/1_000.0) default: fmt.Printf("%d", n) } } func PrettyPrintFloat(f float64) { switch { case math.Abs(f) >= 1_000_000_000_000: fmt.Printf("%.2fT", f/1_000_000_000_000) case math.Abs(f) >= 1_000_000_000: fmt.Printf("%.2fB", f/1_000_000_000) case math.Abs(f) >= 1_000_000: fmt.Printf("%.2fM", f/1_000_000) case math.Abs(f) >= 1_000: fmt.Printf("%.2fK", f/1_000) case math.Abs(f) <= 0.0001: fmt.Printf("%.6f", f) case math.Abs(f) <= 0.001: fmt.Printf("%.5f", f) case math.Abs(f) <= 0.01: fmt.Printf("%.4f", f) case math.Abs(f) <= 0.1: fmt.Printf("%.3f", f) default: fmt.Printf("%.2f", f) } } func PrettyPrint2Floats(low float64, high float64) { PrettyPrintFloat(low) fmt.Printf(" ") PrettyPrintFloat(high) } func ParseFloat(word string) (float64, error) { // l = len(word) // assuming no UTF stuff switch len(word) { case 0: return 0, errors.New("String to be parsed into float must not be the empty string") case 1: return strconv.ParseFloat(word, 64) } n := len(word) - 1 f, err := strconv.ParseFloat(word[:n], 64) if err != nil { return 0, err } multiplier := 1.0 switch word[n] { case '%': multiplier = 0.01 case 'K': multiplier = 1_000 case 'M': multiplier = 1_000_000 case 'B': multiplier = 1_000_000 case 'T': multiplier = 1_000_000 default: multiplier = 1.0 } return f * multiplier, nil }