Navigation Menu

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

Commit

Permalink
fix(numberFilter): format numbers that round to zero as nonnegative
Browse files Browse the repository at this point in the history
Previously when a negative number was rounded to 0 by the number filter
it would be formated as a negative number.  This means something like
{{ -0.01 | number: 1 }} would output -0.0.  Now it will ouput 0.0
instead.

Closes #8489
  • Loading branch information
smilli authored and btford committed Sep 3, 2014
1 parent 73157b0 commit ae952fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ng/filter/filters.js
Expand Up @@ -161,6 +161,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
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 @@ -184,7 +189,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

0 comments on commit ae952fb

Please sign in to comment.