budgit/internal/ui/utils/currency.go
juancwu d747454f4a feat: add space account page
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 03:44:04 +00:00

35 lines
699 B
Go

package utils
import "strings"
func FormatDecimalWithThousands(numStr string) (string, error) {
// Split into integer and decimal parts
parts := strings.SplitN(numStr, ".", 2)
intPart := parts[0]
// Handle negative numbers
negative := false
if strings.HasPrefix(intPart, "-") {
negative = true
intPart = intPart[1:]
}
// Insert thousand separators
var result []byte
for i, c := range intPart {
if i > 0 && (len(intPart)-i)%3 == 0 {
result = append(result, ',')
}
result = append(result, byte(c))
}
// Reassemble
formatted := string(result)
if len(parts) == 2 {
formatted += "." + parts[1]
}
if negative {
formatted = "-" + formatted
}
return formatted, nil
}