Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a currency format pattern from AngularJS filter

I'm interested in creating a filter that would accept an amount, ISO 4217 currency code, and fraction size, and return the amount in that format. I noticed that AngularJS has goog.i18n.currency.getGlobalCurrencyPattern in i18n/closure/currencySymbols.js, but I'm relatively new to Angular and not sure if it is possible to use it?

like image 455
JJ Johnson Avatar asked Sep 11 '13 15:09

JJ Johnson


People also ask

Which AngularJS filter formats a number to a currency format?

AngularJS currency Filter The currency filter formats a number to a currency format. By default, the locale currency format is used.

Which built in filter is used to format the expression as currency in AngularJS?

AngularJS Filters AngularJS provides filters to transform data: currency Format a number to a currency format.

What is the syntax for using currency filter?

AngularJS Currency Filter Syntax E.g Suppose if we give expression like {{1000 | currency}} by default it will format our number and return result like $1,000.00.

Which one is correct way to apply filters in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for the filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.


1 Answers

The currency pattern and symbols are just that. It determines how to display a number. It will not convert it from XXX to XXX. You will have to do the converting part based on the current conversion rates.

As far as using the built-in currency formatting, there are multiple ways: in the template, in the code.

In the template:

<div>{{myCurrencyValue | currency:'XXX'}}</div>

In the code:

var formatted = $filter('currency')(myCurrencyValue, 'XXX')

In both cases, 'XXX' is optional [and is symbol] and will use the default currency for current locale

Example: http://jsfiddle.net/TheSharpieOne/N7YuP/

More information can be found here: Currency Docs

UPDATE

Example with ISO 4217 codes using custom filter: http://jsfiddle.net/TheSharpieOne/N7YuP/3/

Note: ISO 4217 doesn't dictate the currency symbols, I used this for symbol reference and mapped them.

like image 97
TheSharpieOne Avatar answered Sep 22 '22 22:09

TheSharpieOne