From 079535ce6b3ce13a69c840b6aa8ed9542c499f27 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 30 Jun 2024 09:22:04 -0400 Subject: [PATCH] fix bugs float suffix handler --- pretty/pretty.go | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/pretty/pretty.go b/pretty/pretty.go index 1454d13..cf94ba8 100644 --- a/pretty/pretty.go +++ b/pretty/pretty.go @@ -63,28 +63,26 @@ func multiplyOrPassThroughError(a float64, b float64, err error) (float64, error func parseFloat(word string) (float64, error) { // l = len(word) // assuming no UTF stuff - if len(word) == 0 { + switch len(word) { + case 0: return 0, errors.New("String to be parsed into float must not be the empty string") - } else if len(word) == 1 { - return strconv.ParseFloat(word) + case 1: + return strconv.ParseFloat(word, 64) } - n = len(word) - 1 + n := len(word) - 1 + f, err := strconv.ParseFloat(word[:n], 64) switch word[n] { - case "K": - f, err := strconv.ParseFloat(word[:n], 64) + case 'K': return multiplyOrPassThroughError(1_000, f, err) - case "M": - f, err := strconv.ParseFloat(word[:n], 64) + case 'M': return multiplyOrPassThroughError(1_000_000, f, err) - case "B": - f, err := strconv.ParseFloat(word[:n], 64) - return multiplyOrPassThroughError(1_000_000_000, f, err) - case "T": - f, err := strconv.ParseFloat(word[:n], 64) + case 'B': return multiplyOrPassThroughError(1_000_000_000, f, err) + case 'T': + return multiplyOrPassThroughError(1_000_000_000_000, f, err) default: - return strconv.ParseFloat(word) + return f, err } }