|
11 | 11 | *
|
12 | 12 | * @param {number} amount Input to filter.
|
13 | 13 | * @param {string=} symbol Currency symbol or identifier to be displayed.
|
| 14 | + * @param {number=} fractionSize Number of decimal places to round the amount to. |
14 | 15 | * @returns {string} Formatted number.
|
15 | 16 | *
|
16 | 17 | *
|
|
26 | 27 | <input type="number" ng-model="amount"> <br>
|
27 | 28 | default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
|
28 | 29 | custom currency identifier (USD$): <span>{{amount | currency:"USD$"}}</span>
|
| 30 | + no fractions (0): <span>{{amount | currency:"USD$":0}}</span> |
29 | 31 | </div>
|
30 | 32 | </file>
|
31 | 33 | <file name="protractor.js" type="protractor">
|
32 | 34 | it('should init with 1234.56', function() {
|
33 | 35 | expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
|
34 | 36 | 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'); |
35 | 38 | });
|
36 | 39 | it('should update', function() {
|
37 | 40 | if (browser.params.browser == 'safari') {
|
|
43 | 46 | element(by.model('amount')).sendKeys('-1234');
|
44 | 47 | expect(element(by.id('currency-default')).getText()).toBe('($1,234.00)');
|
45 | 48 | expect(element(by.binding('amount | currency:"USD$"')).getText()).toBe('(USD$1,234.00)');
|
| 49 | + expect(binding('amount | currency:"USD$":0')).toBe('(USD$1,234)'); |
46 | 50 | });
|
47 | 51 | </file>
|
48 | 52 | </example>
|
49 | 53 | */
|
50 | 54 | currencyFilter.$inject = ['$locale'];
|
51 | 55 | function currencyFilter($locale) {
|
52 | 56 | var formats = $locale.NUMBER_FORMATS;
|
53 |
| - return function(amount, currencySymbol){ |
| 57 | + return function(amount, currencySymbol, fractionSize){ |
54 | 58 | 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). |
56 | 61 | replace(/\u00A4/g, currencySymbol);
|
57 | 62 | };
|
58 | 63 | }
|
|
0 commit comments