Skip to content

Commit 622f131

Browse files
committed
feat(currencyFilter): add fractionSize as optional parameter
currencyFilter accepts number of decimals to round off to Closes angular#3642
1 parent ea653e4 commit 622f131

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/ng/filter/filters.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* @param {number} amount Input to filter.
1313
* @param {string=} symbol Currency symbol or identifier to be displayed.
14+
* @param {number=} fractionSize Number of decimal places to round the amount to.
1415
* @returns {string} Formatted number.
1516
*
1617
*
@@ -26,12 +27,14 @@
2627
<input type="number" ng-model="amount"> <br>
2728
default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
2829
custom currency identifier (USD$): <span>{{amount | currency:"USD$"}}</span>
30+
no fractions (0): <span>{{amount | currency:"USD$":0}}</span>
2931
</div>
3032
</file>
3133
<file name="protractor.js" type="protractor">
3234
it('should init with 1234.56', function() {
3335
expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
3436
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('USD$1,234.56');
37+
expect(element(by.binding('amount | currency:"USD$":0')).getText()).toBe('USD$1,235');
3538
});
3639
it('should update', function() {
3740
if (browser.params.browser == 'safari') {
@@ -43,16 +46,18 @@
4346
element(by.model('amount')).sendKeys('-1234');
4447
expect(element(by.id('currency-default')).getText()).toBe('($1,234.00)');
4548
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('(USD$1,234.00)');
49+
expect(binding('amount | currency:"USD$":0')).toBe('(USD$1,234)');
4650
});
4751
</file>
4852
</example>
4953
*/
5054
currencyFilter.$inject = ['$locale'];
5155
function currencyFilter($locale) {
5256
var formats = $locale.NUMBER_FORMATS;
53-
return function(amount, currencySymbol){
57+
return function(amount, currencySymbol, fractionSize){
5458
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
55-
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
59+
if (isUndefined(fractionSize) || isNaN(fractionSize) fractionSize = 2;
60+
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
5661
replace(/\u00A4/g, currencySymbol);
5762
};
5863
}

test/ng/filter/filtersSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('filters', function() {
9595
expect(currency(0)).toEqual('$0.00');
9696
expect(currency(-999)).toEqual('($999.00)');
9797
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
98+
expect(currency(1234.5678, "USD$", 0)).toEqual('USD$1,235');
9899
});
99100

100101

0 commit comments

Comments
 (0)