I am trying to format the output of monetary values with moment JS, and using the example on their website '$ 0,0[.]00'
and editing this for pound sterling '£ 0,0[.]00'
only outputs the value, and not the pound sign.
Does numeral not support currencies other than dollars?
The code I am using is:
numeral(200).format('£ 0,0[.]00')
I am trying to format the output of monetary values with moment JS, and using the example on their website '$ 0,0[.]00'
and editing this for pound sterling '£ 0,0[.]00'
only outputs the value, and not the pound sign.
Does numeral not support currencies other than dollars?
The code I am using is:
numeral(200).format('£ 0,0[.]00')
Share
Improve this question
asked May 9, 2016 at 22:03
Le MoiLe Moi
1,0253 gold badges16 silver badges41 bronze badges
1
- Numeral.js uses the current user language as a hint to what localized formatting options are to be used. Is your current language set as 'en-GB' or some other language that would tend to imply the use of pounds? – 1800 INFORMATION Commented May 10, 2016 at 0:08
3 Answers
Reset to default 6At lines 67 and 68 of the un–minified code there is:
// figure out what kind of format we are dealing with
if (format.indexOf('$') > -1) { // currency!!!!!
So yes, it seems "$" is the only currency symbol recognised. You can add the currency symbol separately, e.g.:
var amount = '£' + numeral(2456.01231).format('0,0[.]00');
console.log(amount); // £2,456.01
or extend the library to deal with other currency symbols.
It may be better to use the ISO symbol GBP, which is not ambiguous. There are many currencies that use the £ symbol, as there are many that use $.
Import the locale and then set it manually.
import numeral from 'numeral';
import 'numeral/locales/en-gb';
numeral.locale('en-gb');
numeral('1234.56').format('$0,0.00'); // £1,234.56
Bearing in mind this was originally answered over two years ago, I want to provide an alternative answer. Numeral now supports locales, meaning you can select different currency formats.
What I've done in my code base is:
- Import numeral and require the GB locale
- Define a default format using the GB locale
- Use
numeral(value).format()
to apply the default formatting I've just defined
This is what it looks like:
import * as numeral from 'numeral';
require('numeral/locales/en-gb');
numeral.locale('en-gb');
numeral.defaultFormat('$0,0.00');
numeral(200).format(); // £200.00
numeral(1234).format(); // £1,234.00
numeral(5431.31).format(); // £5,431.31
Note: When specifying numeral.defaultFormat('$0,0.00')
you still need to use a dollar sign. Because of the GB locale, though, numeral will actually render a £
instead. Using '£0,0.00'
as a format won't actually render any currency sign.