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"); +});