diff --git a/src/helpers.js b/src/helpers.js index 022f471..7358771 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -410,6 +410,16 @@ export function decorateResult( result[1] = symbols[result[1]]; } + // Capture the numeric value before formatting; a comma decimal separator + // (via separator or a locale such as de-DE) would otherwise make parseFloat + // read "1,5" as 1 and select the singular unit name. + let numericValue; + if (typeof result[0] === "string") { + numericValue = parseFloat(result[0]); + } else { + numericValue = result[0]; + } + result[0] = applyNumberFormatting( result[0], locale, @@ -427,15 +437,9 @@ export function decorateResult( } else { unit = BYTE; } - let val; - if (typeof result[0] === "string") { - val = parseFloat(result[0]); - } else { - val = result[0]; - } // Determine singular/plural suffix let suffix; - if (val === 1) { + if (numericValue === 1) { suffix = EMPTY; } else { suffix = S; diff --git a/tests/unit/filesize.test.js b/tests/unit/filesize.test.js index 1e4ccda..4cb51cf 100644 --- a/tests/unit/filesize.test.js +++ b/tests/unit/filesize.test.js @@ -283,6 +283,11 @@ describe("filesize", () => { ); assert.strictEqual(filesize(1234567890, { precision: 2, fullform: true }), "1.2 gigabytes"); }); + + it("should keep the plural fullform when a comma decimal separator is used", () => { + assert.strictEqual(filesize(1500000, { fullform: true, separator: "," }), "1,5 megabytes"); + assert.strictEqual(filesize(1500000, { fullform: true, locale: "de-DE" }), "1,5 megabytes"); + }); }); describe("Base and exponent options", () => {