Numeral-js: Multiple currencies are not supported

Created on 8 May 2014  Â·  14Comments  Â·  Source: adamwdraper/Numeral-js

Numeral-js currently has locality and currency coupled together.

It assumes that if I speak a particular language, that I want all of my Numeral objects to be my native currency in my language's format.

In reality, I want to have many Numeral objects of varying currencies displayed to me in my language's format:

> numeral.language();
"en"
> numeral(1000.23).format('$0,0.00 C'); // defaults to 'USD' because of locality
"$1,000.23 USD"
> numeral(1000.23, 'CAD').format('$0,0.00');
"$1,000.23"
> numeral(1000.23, 'EUR').format('$0,0.00');
"€1,000.23"
> numeral(1000.23, 'EUR').format('0,0.00 C');
"1,000.23 EUR"

> numeral.language('fr');
> numeral(1000.23).format('$0,0.00'); // defaults to 'EUR' because of locality
"€1 000,23"
> numeral(1000.23, 'USD').format('$0,0.00');
"$1 000,23"
> numeral(1000.23, 'USD').format('$0,0.00 C');
"$1 000,23 USD" 
> numeral(1000.23, 'CAD').format('$0,0.00');
"$1 000,23"
> numeral(1000.23, 'EUR').format('$0,0.00');
"€1 000,23"
> numeral(1000.23, 'EUR').format('0,0.00 C');
"1 000,23 EUR"

Essentially my language setting should dictate format, and currency should dictate the symbol.

Most helpful comment

Formatting for users locale didn't meet our requirement (needed to show different formatting simultaneously.)

We ended up rolling our own currency formatting solution. It's pretty simple really.

All 14 comments

:+1: I'd like to easily display amounts in multiple different currencies on the same page.

The coupling between the language and the currency symbol is unfortunate indeed. One may want to present the same amount, with an assumed unit (say, 150.99 expressed in euros) to both a French audience (150,99 €) and an English audience (€150.99). Btw, there is another use case, that would involve conversion rates and a base currency (to compute conversions from), but Numeral-js is not about ratings, so… ;)

It's not just multiple currency support. As @chikamichi suggests, currency (3-letter ISO currency code) and language/locale are orthogonal. Language may change how a currency symbol is rendered (e.g. $ vs US$ or Euro symbol before vs after the amount) but should not affect the type of currency represented. Historical changes for a given country may require different currencies and/or symbols.

The way numeral.js currently assumes a currency symbol based on locale is likely to lead to disastrous results in other projects which follow this API. Imagine a user with $100 in the bank with a locale changed they see another currency on the same data... does the value of their account also change?

This issue actually prevents me from using the library. @f3ndot's solution is quite elegant.

:+1: i also need this feature for formatting different amounts in different currencies

Oh what a disaster.

Why can't format() just accept the string we send it? ie. if I sent "€0.0,00" or "$0,0.00" it could format to this string. Locales are great but should be optional.

Oh what a disaster. Does anyone know library that supports a decoupled currency implementation? All I can think of is d3 but really, thats using a shotgun to kill a beetle.

So it seems like setting locales as suggested in the end of the libraries documentation was the way to go. Hope that helps @NathanCH

I was aware, thanks @cozzbie. Registering each currency into a locale was extra boilerplate we didn't have time for. In retrospect I suppose you could define a locale as 'current' but we went with AccountingJS which worked fine for now

This problem also forces us to migrate to a different library. Ditto to above statement: "The coupling between the language and the currency symbol is unfortunate".

Same issue here. I cannot use this library due to this problem. What alternative libraries help support this?

@AlexSwensen Depending on what you want to do, accounting js worked for me (http://openexchangerates.github.io/accounting.js) but similarly to Numeral JS, looks like neither libraries are being maintained

I ended up using https://github.com/smirzaei/currency-formatter for currency formatting, and numeral.js for every other number formatting.

Formatting for users locale didn't meet our requirement (needed to show different formatting simultaneously.)

We ended up rolling our own currency formatting solution. It's pretty simple really.

We are in the same boat, using locale to switch currency symbol is such an odd behavior. We have a need to show multiple currencies on the same page. It absolutely should not be switched by user's locale. We had to use another formatting solution which is unfortunate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kinguru picture kinguru  Â·  6Comments

jfstephe picture jfstephe  Â·  4Comments

MisterGoodcat picture MisterGoodcat  Â·  3Comments

ragulka picture ragulka  Â·  6Comments

Eric24 picture Eric24  Â·  8Comments