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

fix(numberFilter): format numbers that round to zero as nonnegative #8745

Closed
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
number = +(Math.round(+(number.toString() + 'e' + fractionSize)).toString() + 'e' + -fractionSize);

if (number === 0) {
isNegative = false;
}

var fraction = ('' + number).split(DECIMAL_SEP);
var whole = fraction[0];
fraction = fraction[1] || '';
Expand Down
7 changes: 6 additions & 1 deletion test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ describe('filters', function() {
num = formatNumber(123.1, pattern, ',', '.', 3);
expect(num).toBe('123.100');
});

it('should format numbers that round to zero as nonnegative', function(){
var num = formatNumber(-0.01, pattern, ',', '.', 1);
expect(num).toBe('0.0');
});
});

describe('currency', function() {
Expand Down Expand Up @@ -176,7 +181,7 @@ describe('filters', function() {
expect(number(1e-6, 6)).toEqual('0.000001');
expect(number(1e-7, 6)).toEqual('0.000000');

expect(number(-1e-50, 0)).toEqual('-0');
expect(number(-1e-50, 0)).toEqual('0');
expect(number(-1e-6, 6)).toEqual('-0.000001');
expect(number(-1e-7, 6)).toEqual('-0.000000');
});
Expand Down