From 81d427b5f0d3502f65e8db5beaa5ad837c9ede17 Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Thu, 15 May 2014 14:20:47 -0300 Subject: [PATCH] fix(numberFilter): fix rounding error edge case Fix a number rounding error. Closes #7453 Closes #7478 --- src/ng/filter/filters.js | 4 ++-- test/ng/filter/filtersSpec.js | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 5bafa3178907..54aa9baa3cfb 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -145,8 +145,8 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) { fractionSize = Math.min(Math.max(pattern.minFrac, fractionLen), pattern.maxFrac); } - var pow = Math.pow(10, fractionSize); - number = Math.round(number * pow) / pow; + var pow = Math.pow(10, fractionSize + 1); + number = Math.floor(number * pow + 5) / pow; var fraction = ('' + number).split(DECIMAL_SEP); var whole = fraction[0]; fraction = fraction[1] || ''; diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index b4432316ee8b..271691efcefa 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -150,6 +150,10 @@ describe('filters', function() { expect(number(1234.567, 0)).toEqual("1,235"); expect(number(1234.567, 1)).toEqual("1,234.6"); expect(number(1234.567, 2)).toEqual("1,234.57"); + expect(number(1.255, 0)).toEqual("1"); + expect(number(1.255, 1)).toEqual("1.3"); + expect(number(1.255, 2)).toEqual("1.26"); + expect(number(1.255, 3)).toEqual("1.255"); }); it('should filter exponentially large numbers', function() {