Skip to content

Commit f3c6d5d

Browse files
committed
fix zero precision
1 parent a13ebb9 commit f3c6d5d

File tree

8 files changed

+56
-5
lines changed

8 files changed

+56
-5
lines changed

src/formatDecimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export default function(x) {
88
// significant digits p, where x is positive and p is in [1, 21] or undefined.
99
// For example, formatDecimalParts(1.23) returns ["123", 0].
1010
export function formatDecimalParts(x, p) {
11-
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
12-
var i, coefficient = x.slice(0, i);
11+
if (!isFinite(x) || x === 0) return null; // NaN, ±Infinity, ±0
12+
var i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e"), coefficient = x.slice(0, i);
1313

1414
// The string returned by toExponential either has the form \d\.\d+e[-+]\d+
1515
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).

src/formatPrefixAuto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export var prefixExponent;
44

55
export default function(x, p) {
66
var d = formatDecimalParts(x, p);
7-
if (!d) return x + "";
7+
if (!d) return prefixExponent = undefined, x.toPrecision(p);
88
var coefficient = d[0],
99
exponent = d[1],
1010
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,

src/locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default function(locale) {
8787

8888
// Compute the prefix and suffix.
8989
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
90-
valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
90+
valueSuffix = (type === "s" && !isNaN(value) && prefixExponent !== undefined ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
9191

9292
// Break the formatted value into the integer “value” part that can be
9393
// grouped, and fractional or exponential “suffix” part that is not.

test/format-type-r-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {assert, test} from "vitest";
22
import {format} from "../src/index.js";
33

44
test("format(\"r\") can round to significant digits", () => {
5-
assert.strictEqual(format(".2r")(0), "0.0");
65
assert.strictEqual(format(".1r")(0.049), "0.05");
76
assert.strictEqual(format(".1r")(-0.049), "−0.05");
87
assert.strictEqual(format(".1r")(0.49), "0.5");
@@ -32,6 +31,12 @@ test("format(\"r\") can round to significant digits", () => {
3231
assert.strictEqual(format(".15r")(.999999999999999), "0.999999999999999");
3332
});
3433

34+
test("format(\"r\") can round zero", () => {
35+
assert.strictEqual(format(".2r")(0), "0");
36+
assert.strictEqual(format(".1r")(0), "0");
37+
assert.strictEqual(format("r")(0), "0");
38+
});
39+
3540
test("format(\"r\") can round very small numbers", () => {
3641
const f = format(".2r");
3742
assert.strictEqual(f(1e-22), "0.00000000000000000000010");

test/format-type-s-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ test("format(\"s\") outputs SI-prefix notation with default precision 6", () =>
1717
assert.strictEqual(f(.000001), "1.00000µ");
1818
});
1919

20+
test("format(\"s\") does not get confused by NaN, Infinity, etc.", () => {
21+
const f = format("s");
22+
assert.strictEqual(f(999500), "999.500k");
23+
assert.strictEqual(f(Infinity), "Infinity");
24+
assert.strictEqual(f(NaN), "NaN");
25+
});
26+
2027
test("format(\"[.precision]s\") outputs SI-prefix notation with precision significant digits", () => {
2128
const f1 = format(".3s");
2229
assert.strictEqual(f1(0), "0.00");

test/precisionFixed-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@ test("precisionFixed(number) returns the expected value", () => {
99
assert.strictEqual(precisionFixed(0.089), 2);
1010
assert.strictEqual(precisionFixed(0.011), 2);
1111
});
12+
13+
test("precisionFixed(0) returns NaN", () => {
14+
assert.isNaN(precisionFixed(0));
15+
});
16+
17+
test("precisionFixed(NaN) returns NaN", () => {
18+
assert.isNaN(precisionFixed(NaN));
19+
});
20+
21+
test("precisionFixed(Infinity) returns NaN", () => {
22+
assert.isNaN(precisionFixed(Infinity));
23+
assert.isNaN(precisionFixed(-Infinity));
24+
});

test/precisionPrefix-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,16 @@ test("precisionPrefix(step, value) returns the expected precision when value is
4040
assert.strictEqual(precisionPrefix(1e24, 1e27), 0); // 1000Y
4141
assert.strictEqual(precisionPrefix(1e23, 1e27), 1); // 1000.0Y
4242
});
43+
44+
test("precisionPrefix(0, value) returns NaN", () => {
45+
assert.isNaN(precisionPrefix(0, 1));
46+
});
47+
48+
test("precisionPrefix(NaN, value) returns NaN", () => {
49+
assert.isNaN(precisionPrefix(NaN, 1));
50+
});
51+
52+
test("precisionPrefix(Infinity, value) returns NaN", () => {
53+
assert.isNaN(precisionPrefix(Infinity, 1));
54+
assert.isNaN(precisionPrefix(-Infinity, 1));
55+
});

test/precisionRound-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ test("precisionRound(step, max) returns the expected value", () => {
77
assert.strictEqual(precisionRound(0.01, 1.00), 2); // "0.99", "1.0"
88
assert.strictEqual(precisionRound(0.01, 1.01), 3); // "1.00", "1.01"
99
});
10+
11+
test("precisionRound(0, max) returns NaN", () => {
12+
assert.isNaN(precisionRound(0, 1));
13+
});
14+
15+
test("precisionRound(NaN, max) returns NaN", () => {
16+
assert.isNaN(precisionRound(NaN, 1));
17+
});
18+
19+
test("precisionRound(Infinity, max) returns NaN", () => {
20+
assert.isNaN(precisionRound(Infinity, 1));
21+
assert.isNaN(precisionRound(-Infinity, 1));
22+
});

0 commit comments

Comments
 (0)