From 89d97cfaa5f63f55ccb0fd27d9a45b61d63a9193 Mon Sep 17 00:00:00 2001 From: xiehuanyi <74809554+xiehuanyi@users.noreply.github.com> Date: Sat, 10 Oct 2026 20:30:00 +0300 Subject: [PATCH] Use an unscaled formatter for a zero SI-prefix reference AI-assisted implementation and verification using OpenAI Codex. --- src/locale.js | 2 +- test/formatPrefix-test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/locale.js b/src/locale.js index 7a40df5..93e51a9 100644 --- a/src/locale.js +++ b/src/locale.js @@ -132,7 +132,7 @@ export default function(locale) { } function formatPrefix(specifier, value) { - var e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3, + var e = value === 0 ? 0 : Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3, k = Math.pow(10, -e), f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier), {suffix: prefixes[8 + e / 3]}); return function(value) { diff --git a/test/formatPrefix-test.js b/test/formatPrefix-test.js index 69308ef..a9ce489 100644 --- a/test/formatPrefix-test.js +++ b/test/formatPrefix-test.js @@ -37,3 +37,11 @@ test("formatPrefix(\"($~s\", value)(number) formats with the SI prefix inside pa assert.strictEqual(formatPrefix("($~s", 1e3)(1e3), "$1k"); assert.strictEqual(formatPrefix("($~s", 1e3)(-1e3), "($1k)"); }); + +test("formatPrefix uses no SI prefix for a zero reference value", () => { + const f = formatPrefix(".1", 0); + assert.strictEqual(f(0), "0.0"); + assert.strictEqual(f(1), "1.0"); + assert.strictEqual(f(-1), "−1.0"); + assert.strictEqual(formatPrefix(".1", -0)(1), "1.0"); +});