Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Fixing problems with large numbers and formatNumber #12709

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,13 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
fractionSize = Math.min(Math.max(pattern.minFrac, fractionLen), pattern.maxFrac);
}

// safely round numbers in JS without hitting imprecisions of floating-point arithmetics
// inspired by:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
number = +(Math.round(+(number.toString() + 'e' + fractionSize)).toString() + 'e' + -fractionSize);
// round the number only if it's decimal
if (number.toString().indexOf('.') >= 0) {
// safely round numbers in JS without hitting imprecisions of floating-point arithmetics
// inspired by:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
number = +(Math.round(+(number.toString() + 'e' + fractionSize)).toString() + 'e' + -fractionSize);
}

var fraction = ('' + number).split(DECIMAL_SEP);
var whole = fraction[0];
Expand Down
19 changes: 19 additions & 0 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ describe('filters', function() {
expect(formatNumber(-0.0001, pattern, ',', '.', 3)).toBe('0.000');
expect(formatNumber(-0.0000001, pattern, ',', '.', 6)).toBe('0.000000');
});

it('should work with numbers that are close to the limit for exponent notation', function() {
// previously, numbers that n * (10 ^ fractionSize) > localLimitMax
// were ending up with a second exponent in them, then coercing to
// NaN when formatNumber rounded them with the safe rounding
// function.

var localLimitMax = 999999999999999900000,
localLimitMin = 10000000000000000000,
exampleNumber = 444444444400000000000;

expect(formatNumber(localLimitMax, pattern, ',', '.', 2))
.toBe('999,999,999,999,999,900,000.00');
expect(formatNumber(localLimitMin, pattern, ',', '.', 2))
.toBe('10,000,000,000,000,000,000.00');
expect(formatNumber(exampleNumber, pattern, ',', '.', 2))
.toBe('444,444,444,400,000,000,000.00');

});
});

describe('currency', function() {
Expand Down