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

Commit

Permalink
fix(numberFilter): always convert scientific notation to decimal
Browse files Browse the repository at this point in the history
Previously, the number filter would format small and large numbers
as scientific notation. It now uses toFixed() to ensure that all
requested digits are shown.
  • Loading branch information
pmeskers authored and IgorMinar committed Jul 24, 2013
1 parent 454bcfa commit a13c01a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
}

if (fractionSize && fractionSize !== "0") formatedText += decimalSep + fraction.substr(0, fractionSize);
} else {

if (fractionSize > 0 && number > -1 && number < 1) {
formatedText = number.toFixed(fractionSize);
}
}

parts.push(isNegative ? pattern.negPre : pattern.posPre);
Expand Down
21 changes: 18 additions & 3 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,24 @@ describe('filters', function() {
expect(number(1234.567, 2)).toEqual("1,234.57");
});

it('should filter exponential numbers', function() {
expect(number(1e50, 0)).toEqual('1e+50');
expect(number(-2e50, 2)).toEqual('-2e+50');
it('should filter exponentially large numbers', function() {
expect(number(1e50)).toEqual('1e+50');
expect(number(-2e100)).toEqual('-2e+100');
});

it('should ignore fraction sizes for large numbers', function() {
expect(number(1e50, 2)).toEqual('1e+50');
expect(number(-2e100, 5)).toEqual('-2e+100');
});

it('should filter exponentially small numbers', function() {
expect(number(1e-50, 0)).toEqual('0');
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-6, 6)).toEqual('-0.000001');
expect(number(-1e-7, 6)).toEqual('-0.000000');
});
});

Expand Down

0 comments on commit a13c01a

Please sign in to comment.