Extend angular's built in currency filter.
Formats a number as a currency (ie $1,234.56 or 914.3534€). When no currency symbol is provided, default symbol for current locale is used.
Overwrites angular's default currency filter if module: currencyFilter
is injected. (complete example in the Example section)
{{ currency_expression | currency:symbol[:fractionSize[:suffixSymbol[:customFormat]]] }}
$filter('currency')(amount, symbol[, fractionSize[, suffixSymbol[, customFormat]]])
Param | Type | Details |
---|---|---|
amount | number | Input to filter. |
symbol | string | Currency symbol or identifier to be displayed. Falls back to ng.$locale. |
fractionSize | number | Number of decimal places to round the number to. Falls back to ng.$locale |
suffixSymbol | boolean or string | If set to true the currency symbol will be placed after the amount. If passed as a string, will apply currencySymbol as a prefix and suffixSymbol as a suffix |
customFormat | object | Customize group and decimal separators (GROUP_SEP , DECIMAL_SEP ) Both falls back to ng.$locale. |
String: Formatted number.
var formats = {
GROUP_SEP: ' ',
DECIMAL_SEP: ','
};
// With all parameters
expect(currency(1234.4239, '€', 1, true, formats)).toEqual('1 234,4€');
// With all parameters, using string suffix
expect(currency(1234.4239, '€', 1, 'EUR', formats)).toEqual('€1 234,4EUR');
// With missing fraction size
expect(currency(1234.4239, '€', true)).toEqual('1,234.42€');
// With missing fraction size, using string suffix
expect(currency(1234.4239, '€', 'EUR')).toEqual('€1,234.42EUR');
// With fraction size only
expect(currency(1234.4239, '$', 3)).toEqual('$1,234.424');
// Only with symbol
expect(currency(1234.4239, '$')).toEqual('$1,234.42');
// Only with custom group and decimal separators
expect(currency(1234.4239, formats)).toEqual('$1 234,42');
<span ng-bind="price | currency:'€':true"></span> <!-- 1234.42€ -->
<span ng-bind="price | currency:'$':' USD'"></span> <!-- $1234.42 USD -->
angular.module('app', ['currencyFilter']).
controller('Ctrl', function ( $scope, $filter ) {
var currency = $filter('currency');
$scope.price = currency(1234.4239, '€', 0, true); // 1234€
$scope.price = currency(1234.4239, '$', 0, ' USD'); // $1234 USD
});
bower install --save angular-currency-filter
Include src/currency-filter.js
or dist/currency-filter.min.js
to your project.
<script src="/bower_components/angular-currency-filter/dist/currency-filter.min.js"></script>
Don't forget to add currencyFilter
module to your app's dependecies.
$ npm install
$ bower install
$ grunt test
$ grunt build
Functionality verified with unit test with angular versions from v1.2.1
to v1.4.9
.