diff --git a/docs/forward-api.md b/docs/forward-api.md index c5db94b..3559576 100644 --- a/docs/forward-api.md +++ b/docs/forward-api.md @@ -216,7 +216,7 @@ Locale-aware number formatting. T("i18n.numeric.number", 1234567) // "1,234,567" T("i18n.numeric.decimal", 3.14) // "3.14" T("i18n.numeric.percent", 0.85) // "85%" -T("i18n.numeric.bytes", 1536000) // "1.5 MB" +T("i18n.numeric.bytes", 1536000) // "1.46 MB" T("i18n.numeric.ordinal", 3) // "3rd" T("i18n.numeric.ago", 5, "minutes") // "5 minutes ago" ``` @@ -226,7 +226,7 @@ The shorthand `N()` function wraps this namespace: ```go i18n.N("number", 1234567) // "1,234,567" i18n.N("percent", 0.85) // "85%" -i18n.N("bytes", 1536000) // "1.5 MB" +i18n.N("bytes", 1536000) // "1.46 MB" i18n.N("ordinal", 1) // "1st" ``` diff --git a/handler_test.go b/handler_test.go index acac1f1..51fa744 100644 --- a/handler_test.go +++ b/handler_test.go @@ -188,7 +188,7 @@ func TestNumericHandler(t *testing.T) { {"i18n.numeric.ordinal", []any{3}, "3rd"}, {"i18n.numeric.ordinal", []any{11}, "11th"}, {"i18n.numeric.percent", []any{0.85}, "85%"}, - {"i18n.numeric.bytes", []any{int64(1536000)}, "1.5 MB"}, + {"i18n.numeric.bytes", []any{int64(1536000)}, "1.46 MB"}, {"i18n.numeric.ago", []any{5, "minutes"}, "5 minutes ago"}, } diff --git a/i18n.go b/i18n.go index 14ae464..29dc242 100644 --- a/i18n.go +++ b/i18n.go @@ -81,7 +81,7 @@ func CurrentDebug() bool { // // N("number", 1234567) // "1,234,567" // N("percent", 0.85) // "85%" -// N("bytes", 1536000) // "1.5 MB" +// N("bytes", 1536000) // "1.46 MB" // N("ordinal", 1) // "1st" func N(format string, value any) string { return T("i18n.numeric."+format, value) diff --git a/i18n_test.go b/i18n_test.go index 295ed7a..f9fd9b3 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -134,7 +134,7 @@ func TestN_Good(t *testing.T) { }{ {"number", "number", int64(1234567), "1,234,567"}, {"percent", "percent", 0.85, "85%"}, - {"bytes", "bytes", int64(1536000), "1.5 MB"}, + {"bytes", "bytes", int64(1536000), "1.46 MB"}, {"ordinal", "ordinal", 1, "1st"}, } for _, tt := range tests { diff --git a/numbers.go b/numbers.go index c324d29..c58cc44 100644 --- a/numbers.go +++ b/numbers.go @@ -91,7 +91,6 @@ func FormatBytes(bytes int64) string { GB = MB * 1024 TB = GB * 1024 ) - nf := getNumberFormat() var value float64 var unit string switch { @@ -110,16 +109,7 @@ func FormatBytes(bytes int64) string { default: return core.Sprintf("%d B", bytes) } - intPart := int64(value) - fracPart := value - float64(intPart) - if fracPart < 0.05 { - return core.Sprintf("%d %s", intPart, unit) - } - fracDigit := int(math.Round(fracPart * 10)) - if fracDigit == 10 { - return core.Sprintf("%d %s", intPart+1, unit) - } - return core.Sprintf("%d%s%d %s", intPart, nf.DecimalSep, fracDigit, unit) + return FormatDecimalN(value, 2) + " " + unit } // FormatOrdinal formats a number as an ordinal. diff --git a/numbers_test.go b/numbers_test.go index 40717ab..19bc525 100644 --- a/numbers_test.go +++ b/numbers_test.go @@ -100,7 +100,7 @@ func TestFormatBytes(t *testing.T) { {1024, "1 KB"}, {1536, "1.5 KB"}, {1048576, "1 MB"}, - {1536000, "1.5 MB"}, + {1536000, "1.46 MB"}, {1073741824, "1 GB"}, {1099511627776, "1 TB"}, }