Skip to content

Commit

Permalink
fix: fix FormatFloat function
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed May 23, 2024
1 parent 8c17d30 commit f95ad66
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/utils/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"math"
"strings"
)

Expand Down Expand Up @@ -81,6 +82,11 @@ func SizeByteToGB(v *int32) string {
}

func FormatFloat(number float64) string {
isNegative := false
if number < 0 {
isNegative = true
number = math.Abs(number)
}
parts := strings.Split(fmt.Sprintf("%.2f", number), ".")
integerPart := parts[0]
decimalPart := parts[1]
Expand All @@ -92,6 +98,9 @@ func FormatFloat(number float64) string {
}
result = append(result, rune(digit))
}

return fmt.Sprintf("%s.%s", string(result), decimalPart)
if isNegative {
return fmt.Sprintf("-%s.%s", string(result), decimalPart)
} else {
return fmt.Sprintf("%s.%s", string(result), decimalPart)
}
}

0 comments on commit f95ad66

Please sign in to comment.