Numeral-js: Currency symbol shouldn't be part of the locale

Created on 6 Jun 2018  Â·  4Comments  Â·  Source: adamwdraper/Numeral-js

(possibly controversial - but let me explain)

I'm English, if I'm viewing several currencies at once, I want to see consistent formatting in my locale regardless of the currency I'm viewing.

numeral.locale('en-gb');

INPUT => FORMAT => RESULT
'1000' => '$0,0'-> '£1,000' <- viewing a GBP (Sterling) amount
'1000' => '$0,0'-> '£1,000' <- viewing a JPN amount?!?!

If I want the Yen ccy symbol then this requires me to know the locale of the currency for display when (in my case) I already know I'm dealing with Yen.

Any suggestions on how this can be easily achieved?

Thanks,
John

Most helpful comment

Yes I have the exact same problem! The way that currency formatting works is a bit weird.

Example: Display three numbers side by side: £10.00, $20.00, €30.00.
This needs to be added in a test case to the project.

Numeraljs works on the principle of the $ symbol indicating you want a currency format, and then to use the regional settings your browser/computer is in to work out whether to use $,€, £, or ¥. However that does not help you if you are building an app that displays multi-currency.

I think the answer must lie in bypassing the currency format entirely, format as a number, and pass in your own currency symbol. However since $ is a reserved character, this is a problem. I've actually never got it to work properly, and would love to see how to do this. It seems to just strip out the currency symbol entirely. Also $ would need to be escaped.

numeral(2000).format('€0,0.00') => 2,000.00 (expected: €2,000.00)
numeral(2000).format('€0 0.00') => 2,000.00 (expected: €2 000.00)
numeral(2000).format('£0,0.00') => 2,000.00 (expected: £2,000.00)
numeral(2000).format('$0,0.00') => $2,000.00 (Great, but my computer is setup to use £)

All 4 comments

Yes I have the exact same problem! The way that currency formatting works is a bit weird.

Example: Display three numbers side by side: £10.00, $20.00, €30.00.
This needs to be added in a test case to the project.

Numeraljs works on the principle of the $ symbol indicating you want a currency format, and then to use the regional settings your browser/computer is in to work out whether to use $,€, £, or ¥. However that does not help you if you are building an app that displays multi-currency.

I think the answer must lie in bypassing the currency format entirely, format as a number, and pass in your own currency symbol. However since $ is a reserved character, this is a problem. I've actually never got it to work properly, and would love to see how to do this. It seems to just strip out the currency symbol entirely. Also $ would need to be escaped.

numeral(2000).format('€0,0.00') => 2,000.00 (expected: €2,000.00)
numeral(2000).format('€0 0.00') => 2,000.00 (expected: €2 000.00)
numeral(2000).format('£0,0.00') => 2,000.00 (expected: £2,000.00)
numeral(2000).format('$0,0.00') => $2,000.00 (Great, but my computer is setup to use £)

You can do something like this to override currency to $ in whatever locale you're using:

import { get, set } from 'lodash';
let localeName = 'de'; // using German locale as an example - get the locale

const localeData = numeral.localeData(localeName); // get locale config object
if (get(localeData, 'currency.symbol') !== '$') {
  set(localeData, 'currency.symbol', '$');
  localeName = `${localeName}-with-dollar`;
  numeral.register('locale', localeName, localeData); // register custom locale
}
numeral.locale(localeName); // use it

numeral.localeData('en-gb').currency.symbol = '£'

I ditched this library in favour of https://github.com/Mottie/javascript-number-formatter in the end

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MisterGoodcat picture MisterGoodcat  Â·  3Comments

Eric24 picture Eric24  Â·  8Comments

meafmira picture meafmira  Â·  8Comments

rvanmil picture rvanmil  Â·  4Comments

tab00 picture tab00  Â·  8Comments