Skip to content

Commit 45de3bc

Browse files
fix(numbers): limit precision
1 parent 310a280 commit 45de3bc

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/common/round.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const MAX_PRECISION = 20;
2+
13
export default function round(value, precision) {
24
let result = value;
35
let decimals = precision || 0;
@@ -8,5 +10,5 @@ export default function round(value, precision) {
810
result = result.toString().split('e');
911
result = Number(result[0] + 'e' + (result[1] ? (Number(result[1]) - decimals) : -decimals));
1012

11-
return result.toFixed(decimals);
13+
return result.toFixed(Math.min(decimals, MAX_PRECISION));
1214
}

test/numbers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ describe('formatNumber', () => {
5656
expect(formatNumber("foo")).toEqual("foo");
5757
});
5858

59+
it('should limit precision', () => {
60+
const value = 5.4654647884512e+96;
61+
expect(formatNumber(value, '#.#')).toEqual(value.toFixed(20));
62+
});
63+
5964

6065
describe('errors', () => {
6166
currencyData.supplemental.currencyData.region.CUSTOM = [{ XXX: {} }];

0 commit comments

Comments
 (0)