From 705df63c71e9633d82125edcfd9c1c2d10a46b9a Mon Sep 17 00:00:00 2001 From: Rahul Ranjan <11793455+rahul05ranjan@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:30:00 +0000 Subject: [PATCH] fix: apply precision before round for decimal units The README documents that precision overrides round, but round ran first: 999.499 kB became 999.5, toPrecision(3) produced "1.00e+3" and the unit bumped to MB; digits beyond round were lost before toPrecision() could keep them (2.887 -> 2.89 -> "2.890"). Decimal units now feed the un-rounded scaled value to the precision pass; whole bytes still round to integers, so the sub-1-byte outputs are unchanged. Fixes #399 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- src/filesize.js | 13 +++++++++++-- tests/unit/filesize.test.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/filesize.js b/src/filesize.js index ecf4f14..a9eae2e 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -176,8 +176,17 @@ export function filesize( val = valueResult.result; e = valueResult.e; - // Rounding + auto-increment ceiling - const rounded = applyRounding(val, ceil, e, round, roundingFunc, autoExponent); + // Rounding + auto-increment ceiling. + // `precision` takes precedence over `round` for decimal units (e > 0): the + // significant-digit pass below must see the un-rounded scaled value, or a + // boundary value is rounded twice (999.499 kB -> 999.5 -> "1.00e+3" -> a + // premature unit bump) and digits beyond `round` never survive to + // toPrecision(). Whole bytes (e === 0) still round to integers — a byte + // count is an integer quantity, and the sub-1-byte outputs are pinned on it. + const precisionFirst = precisionAdjusted > 0 && e > 0; + const rounded = precisionFirst + ? { value: val, e } + : applyRounding(val, ceil, e, round, roundingFunc, autoExponent); result[0] = rounded.value; e = rounded.e; diff --git a/tests/unit/filesize.test.js b/tests/unit/filesize.test.js index 7295d4d..d8eb432 100644 --- a/tests/unit/filesize.test.js +++ b/tests/unit/filesize.test.js @@ -337,6 +337,19 @@ describe("filesize", () => { assert.strictEqual(filesize(1536000, { precision: 2 }), "1.5 MB"); }); + it("should let precision work on the un-rounded value", () => { + // `round` used to run first, so 999.499 kB became 999.5 and + // toPrecision(3) produced "1.00e+3" — a premature unit bump — and + // digits beyond `round` were lost before toPrecision() saw them (#399). + assert.strictEqual(filesize(1245, { precision: 2 }), "1.2 kB"); + assert.strictEqual(filesize(2887, { precision: 4 }), "2.887 kB"); + assert.strictEqual(filesize(999499, { precision: 3 }), "999 kB"); + assert.strictEqual(filesize(1245, { precision: 2, round: 0 }), "1.2 kB"); + // whole bytes still round to integers + assert.strictEqual(filesize(0.001, { precision: 1 }), "0 B"); + assert.strictEqual(filesize(0.5, { precision: 1 }), "1 B"); + }); + it("should apply locale formatting when precision is set", () => { // A string value from toPrecision ignores toLocaleString, so the // locale's decimal separator was previously dropped (1.2, not 1,2).