|
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 | *
|
|
27 | 28 | <input type="number" ng-model="amount"> <br>
|
28 | 29 | default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
|
29 | 30 | custom currency identifier (USD$): <span>{{amount | currency:"USD$"}}</span>
|
| 31 | + no fractions (0): <span>{{amount | currency:"USD$":0}}</span> |
30 | 32 | </div>
|
31 | 33 | </file>
|
32 | 34 | <file name="protractor.js" type="protractor">
|
33 | 35 | it('should init with 1234.56', function() {
|
34 | 36 | expect(element(by.id('currency-default')).getText()).toBe('$1,234.56');
|
35 | 37 | 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'); |
36 | 39 | });
|
37 | 40 | it('should update', function() {
|
38 | 41 | if (browser.params.browser == 'safari') {
|
|
44 | 47 | element(by.model('amount')).sendKeys('-1234');
|
45 | 48 | expect(element(by.id('currency-default')).getText()).toBe('($1,234.00)');
|
46 | 49 | 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)'); |
47 | 51 | });
|
48 | 52 | </file>
|
49 | 53 | </example>
|
50 | 54 | */
|
51 | 55 | currencyFilter.$inject = ['$locale'];
|
52 | 56 | function currencyFilter($locale) {
|
53 | 57 | 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 | + } |
56 | 67 |
|
57 | 68 | // if null or undefined pass it through
|
58 | 69 | return (amount == null)
|
59 | 70 | ? 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). |
61 | 72 | replace(/\u00A4/g, currencySymbol);
|
62 | 73 | };
|
63 | 74 | }
|
|
0 commit comments