Skip to content

Commit

Permalink
Fix totalLength interpretation with negative numbers for #425
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou authored and BenjaminVanRyseghem committed May 7, 2020
1 parent ba78a5b commit d89da86
Show file tree
Hide file tree
Showing 3 changed files with 18 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
10 changes: 10 additions & 0 deletions tests/src/formatting-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2762,4 +2762,14 @@ describe("formatting", () => {
});
});
});

describe("Regression", () => {
it("Issue 425", () => {
let value = -1.2345;
let format = ",4 a";
let result = formatting.format(numbroStub(value), format);

expect(result).toEqual("-1.234");
});
});
});

0 comments on commit d89da86

Please sign in to comment.