From 13f47853b4c6fe3154b00507b419d7cb687cd33c Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Sun, 15 Nov 2026 23:26:09 +0530 Subject: [PATCH] fix: bump the unit when precision reaches the ceiling, without rounding first applyPrecisionHandling() only moved to the next unit when toPrecision() returned e-notation. Since #404 the value is no longer rounded to `round` before the precision pass, so 999.999 kB with precision 4 gives "1000" and printed "1000 kB" (filesize(999999) prints "1 MB"). Move up when the significant-digit form reaches `ceil` too. When the unit moved, the recomputed value was still rounded to `round` decimals before toPrecision(), the same double rounding #399 fixed in the main path: 1000 KiB printed "0.980 MiB" instead of "0.977 MiB", and with round: 0, 392 B at precision 1 printed "0 kB". Take the new value straight from the input; roundingFunc and round are no longer needed here. Fixes #413 --- docs/TECHNICAL_DOCUMENTATION.md | 7 ++-- src/filesize.js | 2 -- src/helpers.js | 38 +++++---------------- tests/unit/filesize-helpers.test.js | 51 +++++------------------------ tests/unit/filesize.test.js | 34 +++++++++++++++++-- 5 files changed, 52 insertions(+), 80 deletions(-) diff --git a/docs/TECHNICAL_DOCUMENTATION.md b/docs/TECHNICAL_DOCUMENTATION.md index e7aeccba..60c8e788 100644 --- a/docs/TECHNICAL_DOCUMENTATION.md +++ b/docs/TECHNICAL_DOCUMENTATION.md @@ -482,12 +482,13 @@ Dispatches the formatted result array into the requested output type. Handles `A - `symbol`: `result[1]` — what appears in string/array output - `unit`: `u` — the base unit before customization -#### `applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, roundingFunc, round, exponent)` +#### `applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, exponent)` -Handles significant digit formatting via `Number.prototype.toPrecision()`. Corrects scientific notation by recalculating with incremented exponent. +Handles significant digit formatting via `Number.prototype.toPrecision()`. Corrects scientific notation and unit overflow by recalculating with incremented exponent. **Key behavior:** -- `toPrecision()` can produce strings like `"1e+21"` — if detected and `e < 8`, recalculates with `e + 1` +- `toPrecision()` can produce strings like `"1e+21"`, or reach the unit ceiling (`"1000"` kB, `"1024"` KiB) — if detected and `e < 8`, recalculates with `e + 1` +- The recalculated value is not rounded to `round` first, so `precision` keeps every significant digit - Only auto-increments in auto-exponent mode #### `applyNumberFormatting(value, locale, localeOptions, separator, pad, round)` diff --git a/src/filesize.js b/src/filesize.js index a9eae2eb..309589bf 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -200,8 +200,6 @@ export function filesize( isDecimal, bits, ceil, - roundingFunc, - round, exponent, ); result[0] = precisionResult.value; diff --git a/src/helpers.js b/src/helpers.js index 0b4c1778..10365292 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -260,7 +260,7 @@ export function calculateBigIntValue(num, e, isDecimal, bits, ceil, autoExponent } /** - * Optimized precision handling with scientific notation correction + * Optimized precision handling with unit overflow correction * @param {number} value - Current value * @param {number} precision - Precision to apply * @param {number} e - Current exponent @@ -268,23 +268,10 @@ export function calculateBigIntValue(num, e, isDecimal, bits, ceil, autoExponent * @param {boolean} isDecimal - Whether using decimal base * @param {boolean} bits - Whether calculating bits * @param {number} ceil - Ceiling value - * @param {Function} roundingFunc - Rounding function - * @param {number} round - Round value * @param {number} exponent - Forced exponent (-1 for auto) * @returns {Object} Object with value and e properties */ -export function applyPrecisionHandling( - value, - precision, - e, - num, - isDecimal, - bits, - ceil, - roundingFunc, - round, - exponent, -) { +export function applyPrecisionHandling(value, precision, e, num, isDecimal, bits, ceil, exponent) { if (typeof value === "string") { value = parseFloat(value); } @@ -304,23 +291,14 @@ export function applyPrecisionHandling( const autoExponent = exponent === -1 || isNaN(exponent); - // Handle scientific notation by recalculating with incremented exponent - if (result.includes(E) && e < 8 && autoExponent) { + // Move to the next unit when the significant digits overflow into + // scientific notation ("1.00e+3") or reach the unit ceiling ("1000 kB", + // "1024 KiB"). The new value is taken straight from the input so + // `precision` sees every digit, as it does in the main path. + if ((result.includes(E) || Number(result) >= ceil) && e < 8 && autoExponent) { e++; const { result: valueResult } = calculateOptimizedValue(num, e, isDecimal, bits, ceil); - let p; - if (round > 0) { - p = Math.pow(10, round); - } else { - p = 1; - } - let computed; - if (p === 1) { - computed = roundingFunc(valueResult); - } else { - computed = roundingFunc(valueResult * p) / p; - } - result = computed.toPrecision(precision); + result = valueResult.toPrecision(precision); } return { value: result, e }; diff --git a/tests/unit/filesize-helpers.test.js b/tests/unit/filesize-helpers.test.js index e9d8c008..b5acd79f 100644 --- a/tests/unit/filesize-helpers.test.js +++ b/tests/unit/filesize-helpers.test.js @@ -145,65 +145,32 @@ describe("Helper Functions", () => { describe("applyPrecisionHandling", () => { it("should apply precision without scientific notation", () => { - const result = applyPrecisionHandling(1.5, 2, 1, 1024, false, false, 1024, Math.round, 2, -1); + const result = applyPrecisionHandling(1.5, 2, 1, 1024, false, false, 1024, -1); assert.deepStrictEqual(result, { value: "1.5", e: 1 }); }); it("should handle scientific notation with increment when exponent is -1", () => { // Test with a large number that would produce scientific notation - const result = applyPrecisionHandling( - 1000000000000, - 1, - 3, - 1e15, - true, - false, - 1000, - Math.round, - 2, - -1, - ); + const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, -1); assert(typeof result.value === "string"); assert(typeof result.e === "number"); }); it("should handle scientific notation with increment when exponent is NaN", () => { // Test that NaN also triggers auto-increment like -1 - const result = applyPrecisionHandling( - 1000000000000, - 1, - 3, - 1e15, - true, - false, - 1000, - Math.round, - 2, - NaN, - ); + const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, NaN); assert(typeof result.value === "string"); assert(typeof result.e === "number"); }); it("should not increment when exponent is forced", () => { // Test that forced exponent prevents auto-increment - const result = applyPrecisionHandling( - 1000000000000, - 1, - 3, - 1e15, - true, - false, - 1000, - Math.round, - 2, - 5, - ); + const result = applyPrecisionHandling(1000000000000, 1, 3, 1e15, true, false, 1000, 5); assert.deepStrictEqual(result, { value: "1e+12", e: 3 }); }); it("should not increment when e >= 8", () => { - const result = applyPrecisionHandling(1.5, 2, 8, 1024, false, false, 1024, Math.round, 2, -1); + const result = applyPrecisionHandling(1.5, 2, 8, 1024, false, false, 1024, -1); assert.deepStrictEqual(result, { value: "1.5", e: 8 }); }); }); @@ -335,7 +302,7 @@ describe("handleZeroValue with explicit symbol", () => { describe("applyPrecisionHandling with string value", () => { it("should parse string value and apply precision", () => { - const result = applyPrecisionHandling("1.5", 2, 1, 1024, false, false, 1024, Math.round, 2, -1); + const result = applyPrecisionHandling("1.5", 2, 1, 1024, false, false, 1024, -1); assert.deepStrictEqual(result, { value: "1.5", e: 1 }); }); @@ -504,20 +471,20 @@ describe("formatOutput edge cases", () => { describe("applyPrecisionHandling validation", () => { it("should throw TypeError for out-of-range precision", () => { assert.throws( - () => applyPrecisionHandling(1.5, 101, 1, 1024, false, false, 1024, Math.round, 2, -1), + () => applyPrecisionHandling(1.5, 101, 1, 1024, false, false, 1024, -1), TypeError, ); }); it("should throw TypeError for non-numeric precision", () => { assert.throws( - () => applyPrecisionHandling(1.5, "foo", 1, 1024, false, false, 1024, Math.round, 2, -1), + () => applyPrecisionHandling(1.5, "foo", 1, 1024, false, false, 1024, -1), TypeError, ); }); it("should floor non-integer precision", () => { - const result = applyPrecisionHandling(1.5, 2.5, 1, 1024, false, false, 1024, Math.round, 2, -1); + const result = applyPrecisionHandling(1.5, 2.5, 1, 1024, false, false, 1024, -1); assert.strictEqual(result.value, "1.5"); }); }); diff --git a/tests/unit/filesize.test.js b/tests/unit/filesize.test.js index 2893caee..c3d66d5f 100644 --- a/tests/unit/filesize.test.js +++ b/tests/unit/filesize.test.js @@ -305,7 +305,7 @@ describe("filesize", () => { it("should handle fullform with precision (string value path)", () => { assert.strictEqual( filesize(1058223158, { standard: "iec", precision: 3, fullform: true }), - "0.990 gibibytes", + "0.986 gibibytes", ); assert.strictEqual(filesize(1234567890, { precision: 2, fullform: true }), "1.2 gigabytes"); }); @@ -350,6 +350,34 @@ describe("filesize", () => { assert.strictEqual(filesize(0.5, { precision: 1 }), "1 B"); }); + it("should move to the next unit when precision reaches the unit ceiling", () => { + // 999.999 kB to 4 significant digits is "1000"; it must not print as + // "1000 kB" when the same value without precision prints "1 MB". + assert.strictEqual(filesize(999999, { precision: 4 }), "1.000 MB"); + assert.strictEqual(filesize(999999, { precision: 5 }), "1.0000 MB"); + assert.strictEqual(filesize(-999999, { precision: 4 }), "-1.000 MB"); + assert.strictEqual(filesize(1048575, { base: 2, precision: 4 }), "1.000 MiB"); + assert.strictEqual(filesize(124999.99, { bits: true, precision: 4 }), "1.000 Mbit"); + assert.deepStrictEqual(filesize(999999, { precision: 4, output: "object" }), { + value: "1.000", + symbol: "MB", + exponent: 2, + unit: "MB", + }); + assert.strictEqual(filesize(999999, { precision: 4, output: "exponent" }), 2); + // a forced exponent keeps the unit + assert.strictEqual(filesize(999999, { precision: 4, exponent: 1 }), "1000 kB"); + }); + + it("should keep every significant digit after moving to the next unit", () => { + // 1000 KiB is 0.9766 MiB; the value used to be rounded to `round` + // decimals (0.98) before toPrecision(3), printing 0.980. + assert.strictEqual(filesize(1024000, { base: 2, precision: 3 }), "0.977 MiB"); + assert.strictEqual(filesize(1034240, { base: 2, precision: 3 }), "0.986 MiB"); + assert.strictEqual(filesize(128000, { base: 2, bits: true, precision: 3 }), "0.977 Mibit"); + assert.strictEqual(filesize(999500, { precision: 3 }), "1.00 MB"); + }); + 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). @@ -588,10 +616,10 @@ describe("filesize", () => { it("should handle precision with specific test case for GiB values", () => { // Test specific precision case: filesize(1058223158, {base: 2, standard: 'iec', precision: 3}) - // This should output '0.990 GiB' + // 1058223158 B = 1009.2 MiB = 0.9855 GiB, so three significant digits are 0.986 assert.strictEqual( filesize(1058223158, { base: 2, standard: "iec", precision: 3 }), - "0.990 GiB", + "0.986 GiB", ); });