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

fix(currency): Handle not-quite-zero values #1477

Closed
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
13 changes: 11 additions & 2 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,18 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
formatedText = '',
parts = [];

var hasExponent = false;
if (numStr.indexOf('e') !== -1) {
formatedText = numStr;
} else {
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/);
if (match && match[2] == '-' && match[3] > fractionSize + 1) {
numStr = '0';
} else {
formatedText = numStr;
hasExponent = true;
}
}

if (!hasExponent) {
var fractionLen = (numStr.split(DECIMAL_SEP)[1] || '').length;

// determine fractionSize if it is not specified
Expand Down
7 changes: 7 additions & 0 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ describe('filters', function() {
expect(currency()).toBe('');
expect(currency('abc')).toBe('');
});

it('should handle zero and nearly-zero values properly', function() {
// This expression is known to yield 4.440892098500626e-16 instead of 0.0.
expect(currency(1.07 + 1 - 2.07)).toBe('$0.00');
expect(currency(0.008)).toBe('$0.01');
expect(currency(0.003)).toBe('$0.00');
});
});


Expand Down