commit 079535ce6b3ce13a69c840b6aa8ed9542c499f27
parent 10dbda7986c7768cd7bb089b3403aa14aadb222c
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date: Sun, 30 Jun 2024 09:22:04 -0400
fix bugs float suffix handler
Diffstat:
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git 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
}
}