Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/formatPrefixAuto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ export default function(x, p) {
if (!d) return prefixExponent = undefined, x.toPrecision(p);
var coefficient = d[0],
exponent = d[1],
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
n = coefficient.length;
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1;

// Below the smallest prefix (yocto), the value is shown as “0.…”, which
// leaves fewer significant digits; re-round to that reduced precision. This
// rounding can carry into a higher power of ten (e.g. 9.99 → 10), so derive
// the digit position from the re-rounded exponent rather than the original.
if (i <= 0) {
d = formatDecimalParts(x, Math.max(0, p + i - 1));
coefficient = d[0];
i = d[1] - prefixExponent + 1;
}

var n = coefficient.length;
return i === n ? coefficient
: i > n ? coefficient + new Array(i - n + 1).join("0")
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
: "0." + new Array(1 - i).join("0") + formatDecimalParts(x, Math.max(0, p + i - 1))[0]; // less than 1y!
: "0." + new Array(1 - i).join("0") + coefficient; // less than 1y!
}
8 changes: 8 additions & 0 deletions test/format-type-s-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ test("format(\"s\") formats numbers smaller than 1e-24 with yocto", () => {
assert.strictEqual(f(-1.29e-21), "−1.2900000z");
});

test("format(\"s\") carries into a higher power when rounding below 1e-24", () => {
assert.strictEqual(format(".3s")(9.99e-25), "1.0y"); // 0.999y → 1.0y, not 0.10y
assert.strictEqual(format(".2s")(9.9e-25), "1y"); // 0.99y → 1y, not 0.1y
assert.strictEqual(format(".3s")(9.99e-26), "0.1y"); // 0.0999y → 0.1y, not 0.01y
assert.strictEqual(format(".1s")(9.5e-25), "1y");
assert.strictEqual(format(".5s")(9.9999e-25), "1.000y");
});

test("format(\"s\") formats numbers larger than 1e24 with yotta", () => {
const f = format(".8s");
assert.strictEqual(f(1.23e+21), "1.2300000Z");
Expand Down