最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Numeral.js - is there no support of pound sterling? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

At 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:

  1. Import numeral and require the GB locale
  2. Define a default format using the GB locale
  3. 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.

发布评论

评论列表(0)

  1. 暂无评论