From f5835963d5982003a713dd354eefd376ed39ac02 Mon Sep 17 00:00:00 2001 From: Kury Kruitbosch Date: Mon, 11 Feb 2013 14:21:15 -0700 Subject: [PATCH] fix(numberFilter): fix formatting when "0" passed as fractionSize When checking to add decimal and trialing 0s number filter used to check trueness of fractionSize. "0" evaluating to true causes "123" to return "123." --- src/ng/filter/filters.js | 2 +- test/ng/filter/filtersSpec.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 4680a30681e4..0b4a2e91e9b8 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -168,7 +168,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) { fraction += '0'; } - if (fractionSize) formatedText += decimalSep + fraction.substr(0, fractionSize); + if (fractionSize && fractionSize !== "0") formatedText += decimalSep + fraction.substr(0, fractionSize); } parts.push(isNegative ? pattern.negPre : pattern.posPre); diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index 766df9895de1..071902a9aa7f 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -71,6 +71,17 @@ describe('filters', function() { var num = formatNumber(123.1116, pattern, ',', '.'); expect(num).toBe('123.112'); }); + + it('should format the same with string as well as numeric fractionSize', function(){ + var num = formatNumber(123.1, pattern, ',', '.', "0"); + expect(num).toBe('123'); + var num = formatNumber(123.1, pattern, ',', '.', 0); + expect(num).toBe('123'); + var num = formatNumber(123.1, pattern, ',', '.', "3"); + expect(num).toBe('123.100'); + var num = formatNumber(123.1, pattern, ',', '.', 3); + expect(num).toBe('123.100'); + }); }); describe('currency', function() {