fix(i18n): handle min int formatting edge case
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 07:19:53 +00:00
parent 06a5fe9ac8
commit 495e977a6f
2 changed files with 25 additions and 3 deletions

View file

@ -165,10 +165,15 @@ func formatIntWithSep(n int64, sep string) string {
return strconv.FormatInt(n, 10)
}
negative := n < 0
var abs uint64
if negative {
n = -n
// Convert via n+1 to avoid overflowing on math.MinInt64.
abs = uint64(-(n + 1))
abs++
} else {
abs = uint64(n)
}
str := strconv.FormatInt(n, 10)
str := strconv.FormatUint(abs, 10)
if len(str) <= 3 {
if negative {
return "-" + str

View file

@ -1,6 +1,9 @@
package i18n
import "testing"
import (
"math"
"testing"
)
func TestFormatNumber(t *testing.T) {
// Ensure service is initialised for English locale
@ -31,6 +34,20 @@ func TestFormatNumber(t *testing.T) {
}
}
func TestFormatNumber_MinInt64(t *testing.T) {
svc, err := New()
if err != nil {
t.Fatalf("New() failed: %v", err)
}
SetDefault(svc)
got := FormatNumber(math.MinInt64)
want := "-9,223,372,036,854,775,808"
if got != want {
t.Fatalf("FormatNumber(math.MinInt64) = %q, want %q", got, want)
}
}
func TestFormatDecimal(t *testing.T) {
svc, err := New()
if err != nil {