diff --git a/CHANGELOG.md b/CHANGELOG.md index 072ece44..a68e6d1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Bump `lodash.template` to 4.5.0 - Fix #359: Add Bytes I18N possibility + French translation - Fix #474: Fix Bytes formatting tests and coverage +- Fix #425: Fix interpretation of totalLength with negative numbers. Thanks @DamienCassou ### 2.2.0 diff --git a/src/formatting.js b/src/formatting.js index e0db816a..96950389 100644 --- a/src/formatting.js +++ b/src/formatting.js @@ -371,8 +371,14 @@ function computeAverage({value, forceAverage, abbreviations, spaceSeparated = fa } if (totalLength) { + let isNegative = value < 0; let characteristic = value.toString().split(".")[0]; - mantissaPrecision = Math.max(totalLength - characteristic.length, 0); + + let characteristicLength = isNegative + ? characteristic.length - 1 + : characteristic.length; + + mantissaPrecision = Math.max(totalLength - characteristicLength, 0); } return {value, abbreviation, mantissaPrecision}; diff --git a/tests/src/formatting-tests.js b/tests/src/formatting-tests.js index 9c62030d..94812654 100644 --- a/tests/src/formatting-tests.js +++ b/tests/src/formatting-tests.js @@ -2711,6 +2711,8 @@ describe("formatting", () => { // [18823578.85, "6 a", "18823.6 k"], // [188235773.85, "6 a", "188236 k"], + [-1.2345, ",4 a", "-1.234"], + // large numbers [100, "0,0[.]0000", "100"], [1e23, "0,0[.]0000", "100,000,000,000,000,000,000,000"],