Jimo
Jimo

Reputation: 163

How to specify locale different from default for currency with Angular currency filter?

In our app we are using angular for i18n/l10n. It's working fine.

There's a problem though - sometimes the user needs to see info aligned with a locale, different from the current one. More specific - currency. E.g. - the user's locale is de-de and they open a catalog with item prices that are in US dollars. Of course, I can specify the currency symbol explicitly:

{{item.Price.Value | currency : "$" : 2 }}

but this is not a very graceful solution, because the user will see:

349,99 $

which is not the correct formatting. The correct one would be:

$349.99

So, I don't want to just change the currency symbol, but to change the whole currency formatting for a specific field and leave the remaining on the page localized according to the current locale.

Is it possible with the Angular currency filter or should I use my own custom filter?

Upvotes: 2

Views: 1404

Answers (1)

Eleanor Zimmermann
Eleanor Zimmermann

Reputation: 432

After looking through the source code, I determined that Angular is using 'undefined' as the parameter trigger for the filter using default local currency. (see around line 14370 in Angular 1.2.15)

Thus, when chaining filters, you can get that default value by providing undefined as the parameter.

For you, the following should give you what you want:

{{item.Price.Value | currency : undefined : 2 }}

That being said, 2 decimal places, which is what you have specified here, IS the Angular default. You'd also get 2 decimal places with the following filter:

{{item.Price.Value | currency}}

Upvotes: 0

Related Questions