From 495e977a6ff4606be2d8c1196757cf64ffb05277 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 07:19:53 +0000 Subject: [PATCH] fix(i18n): handle min int formatting edge case Co-Authored-By: Virgil --- numbers.go | 9 +++++++-- numbers_test.go | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/numbers.go b/numbers.go index 551844e..55483fd 100644 --- a/numbers.go +++ b/numbers.go @@ -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 diff --git a/numbers_test.go b/numbers_test.go index 1b20313..33ebfd3 100644 --- a/numbers_test.go +++ b/numbers_test.go @@ -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 {