Skip to content

Commit 2a03701

Browse files
park9140petebacondarwin
authored andcommitted
feat(currencyFilter): allow fractionSize to be specified
It should pass through the currency number pattern from the current locale Closes angular#5674
1 parent 3b3c8bd commit 2a03701

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/ng/filter/filters.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*
1212
* @param {number} amount Input to filter.
1313
* @param {string=} symbol Currency symbol or identifier to be displayed.
14+
* @param {(number|string)=} fractionSize Number of decimal places to round the number to.
15+
* If this is not provided then the fraction size is computed from the current locale's number
16+
* formatting pattern. In the case of the default locale, it will be 3.
1417
* @returns {string} Formatted number.
1518
*
1619
*
@@ -51,9 +54,10 @@
5154
currencyFilter.$inject = ['$locale'];
5255
function currencyFilter($locale) {
5356
var formats = $locale.NUMBER_FORMATS;
54-
return function(amount, currencySymbol){
57+
return function(amount, currencySymbol, fractionSize){
5558
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
56-
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
59+
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP,
60+
fractionSize).
5761
replace(/\u00A4/g, currencySymbol);
5862
};
5963
}
@@ -108,7 +112,6 @@ function currencyFilter($locale) {
108112
</example>
109113
*/
110114

111-
112115
numberFilter.$inject = ['$locale'];
113116
function numberFilter($locale) {
114117
var formats = $locale.NUMBER_FORMATS;

test/ng/filter/filtersSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ describe('filters', function() {
131131
expect(currency(0.008)).toBe('$0.01');
132132
expect(currency(0.003)).toBe('$0.00');
133133
});
134+
135+
it("should pass through the currency number format pattern from the current locale", inject(function($locale) {
136+
$locale.NUMBER_FORMATS.PATTERNS[1].maxFrac = 1;
137+
expect(currency(123.123, '$')).toBe('$123.1');
138+
$locale.NUMBER_FORMATS.PATTERNS[1].maxFrac = 3;
139+
expect(currency(123.123, '$')).toBe('$123.123');
140+
}));
141+
142+
it('should handle overriding the default locale fractionSize', function() {
143+
expect(currency(0.008, "$", 3)).toBe('$0.008');
144+
expect(currency(0.008, "$", 0)).toBe('$0');
145+
});
134146
});
135147

136148

0 commit comments

Comments
 (0)