fix(i18n): handle min int formatting edge case
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
06a5fe9ac8
commit
495e977a6f
2 changed files with 25 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue