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

feat(currencyFilter): add fractionSize as optional parameter #7922

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* @param {number} amount Input to filter.
* @param {string=} symbol Currency symbol or identifier to be displayed.
* @param {number=} fractionSize Number of decimal places to round the amount to.
* @returns {string} Formatted number.
*
*
Expand All @@ -26,12 +27,14 @@
<input type="number" ng-model="amount"> <br>
default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
custom currency identifier (USD$): <span>{{amount | currency:"USD$"}}</span>
no fractions (0): <span>{{amount | currency:"USD$":0}}</span>
</div>
</file>
<file name="protractor.js" type="protractor">
it('should init with 1234.56', function() {
expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('USD$1,234.56');
expect(element(by.binding('amount | currency:"USD$":0')).getText()).toBe('USD$1,235');
});
it('should update', function() {
if (browser.params.browser == 'safari') {
Expand All @@ -43,16 +46,18 @@
element(by.model('amount')).sendKeys('-1234');
expect(element(by.id('currency-default')).getText()).toBe('($1,234.00)');
expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('(USD$1,234.00)');
expect(element(by.binding('amount | currency:"USD$":0')).getText()).toBe('(USD$1,234)');
});
</file>
</example>
*/
currencyFilter.$inject = ['$locale'];
function currencyFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(amount, currencySymbol){
return function(amount, currencySymbol, fractionSize){
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
if (isUndefined(fractionSize) || isNaN(fractionSize)) fractionSize = 2;
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
replace(/\u00A4/g, currencySymbol);
};
}
Expand Down
1 change: 1 addition & 0 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('filters', function() {
expect(currency(0)).toEqual('$0.00');
expect(currency(-999)).toEqual('($999.00)');
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
expect(currency(1234.5678, "USD$", 0)).toEqual('USD$1,235');
});


Expand Down