mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
feat(currencyFilter): add fractionSize as optional parameter
currencyFilter accepts number of decimals to round off to Closes #3642 Closes #3461 Closes #3642 Closes #7922
This commit is contained in:
@@ -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.
|
||||
*
|
||||
*
|
||||
@@ -27,12 +28,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') {
|
||||
@@ -44,6 +47,7 @@
|
||||
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>
|
||||
@@ -51,13 +55,20 @@
|
||||
currencyFilter.$inject = ['$locale'];
|
||||
function currencyFilter($locale) {
|
||||
var formats = $locale.NUMBER_FORMATS;
|
||||
return function(amount, currencySymbol){
|
||||
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
|
||||
return function(amount, currencySymbol, fractionSize){
|
||||
if (isUndefined(currencySymbol)) {
|
||||
currencySymbol = formats.CURRENCY_SYM;
|
||||
}
|
||||
|
||||
if (isUndefined(fractionSize)) {
|
||||
// TODO: read the default value from the locale file
|
||||
fractionSize = 2;
|
||||
}
|
||||
|
||||
// if null or undefined pass it through
|
||||
return (amount == null)
|
||||
? amount
|
||||
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
|
||||
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize).
|
||||
replace(/\u00A4/g, currencySymbol);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -101,6 +101,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');
|
||||
});
|
||||
|
||||
it('should pass through null and undefined to be compatible with one-time binding', function() {
|
||||
|
||||
Reference in New Issue
Block a user