Skip to content

Commit

Permalink
Fix totalLength interpretation with negative numbers for BenjaminVanR…
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed May 7, 2020
1 parent ba78a5b commit 2239569
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 2 additions & 0 deletions tests/src/formatting-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 2239569

Please sign in to comment.