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

Commit 20685ff

Browse files
doshpromptIgorMinar
authored andcommitted
feat(currencyFilter): add fractionSize as optional parameter
currencyFilter accepts number of decimals to round off to Closes #3642 Closes #3461 Closes #3642 Closes #7922
1 parent 9ba24c5 commit 20685ff

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/ng/filter/filters.js

+14-3
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
*
@@ -27,12 +28,14 @@
2728
<input type="number" ng-model="amount"> <br>
2829
default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
2930
custom currency identifier (USD$): <span>{{amount | currency:"USD$"}}</span>
31+
no fractions (0): <span>{{amount | currency:"USD$":0}}</span>
3032
</div>
3133
</file>
3234
<file name="protractor.js" type="protractor">
3335
it('should init with 1234.56', function() {
3436
expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
3537
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('USD$1,234.56');
38+
expect(element(by.binding('amount | currency:"USD$":0')).getText()).toBe('USD$1,235');
3639
});
3740
it('should update', function() {
3841
if (browser.params.browser == 'safari') {
@@ -44,20 +47,28 @@
4447
element(by.model('amount')).sendKeys('-1234');
4548
expect(element(by.id('currency-default')).getText()).toBe('($1,234.00)');
4649
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('(USD$1,234.00)');
50+
expect(element(by.binding('amount | currency:"USD$":0')).getText()).toBe('(USD$1,234)');
4751
});
4852
</file>
4953
</example>
5054
*/
5155
currencyFilter.$inject = ['$locale'];
5256
function currencyFilter($locale) {
5357
var formats = $locale.NUMBER_FORMATS;
54-
return function(amount, currencySymbol){
55-
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
58+
return function(amount, currencySymbol, fractionSize){
59+
if (isUndefined(currencySymbol)) {
60+
currencySymbol = formats.CURRENCY_SYM;
61+
}
62+
63+
if (isUndefined(fractionSize)) {
64+
// TODO: read the default value from the locale file
65+
fractionSize = 2;
66+
}
5667

5768
// if null or undefined pass it through
5869
return (amount == null)
5970
? amount
60-
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
71+
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
6172
replace(/\u00A4/g, currencySymbol);
6273
};
6374
}

test/ng/filter/filtersSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('filters', function() {
101101
expect(currency(0)).toEqual('$0.00');
102102
expect(currency(-999)).toEqual('($999.00)');
103103
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
104+
expect(currency(1234.5678, "USD$", 0)).toEqual('USD$1,235');
104105
});
105106

106107
it('should pass through null and undefined to be compatible with one-time binding', function() {

0 commit comments

Comments
 (0)