diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 91cb588aff75..8c80ed5a092f 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -118,6 +118,12 @@ function numberFilter($locale) { }; } +function getExponentString(numberString,fractionSize) { + var numberArray = numberString.split('e'), + fractionUsed = +fractionSize; + return (numberArray[0] + 'e' + (numberArray[1] ? (+numberArray[1] + fractionUsed) : fractionUsed)); +} + var DECIMAL_SEP = '.'; function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) { if (number == null || !isFinite(number) || isObject(number)) return ''; @@ -151,7 +157,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) { // 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); + number = +(getExponentString(Math.round(+(getExponentString(number.toString(),fractionSize))).toString(),-fractionSize)); var fraction = ('' + number).split(DECIMAL_SEP); var whole = fraction[0]; diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index af0cdba6d96d..bee7cf6c40c8 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -59,6 +59,12 @@ describe('filters', function() { expect(num).toBe('1.1112'); }); + it('should format large number',function() { + pattern.gsize = 2; + var num = formatNumber(12345868059685210000, pattern, ',', '.', 2); + expect(num).toBe('12,345,868,059,685,210,000.00'); + }); + it('should format according different separators', function() { var num = formatNumber(1234567.1, pattern, '.', ',', 2); expect(num).toBe('1.234.567,10');