使用数字.js设置大数字的格式



在我的应用程序中,我想使用库格式化各种数字,并且我有几个相关的问题(我没有单独提交,因为我认为它们可能代表一组非常常见的问题(

  1. 使用格式字符串常量设置数字格式以实现压缩文本,例如1.2k1.23M
  2. 使用格式字符串常量设置数字格式,以应用一千个分隔符,不受客户端区域设置的影响。

我试图实现格式化结果,其中实际上考虑了语言千分隔符

http://jsfiddle.net/erbronni/19mLmekt/

// load a language
numeral.language('fr', {
    delimiters: {
        thousands: ' ',
        decimal: ','
    },
    abbreviations: {
        thousand: 'k',
        million: 'M',
        billion: '',
        trillion: 't'
    },
    ordinal : function (number) {
        return number === 1 ? 'er' : 'ème';
    },
    currency: {
        symbol: '€'
    }
});
numeral.language('fr');
document.getElementById('f1').innerHTML = numeral(12345678).format('0 000') // intended output: '12 345 678' -- does not seem to work

Numeral.js内置了这个。使用.format('0.00a')a可以轻松实现。

一些完整的例子:

numeral(1000000).format('0a')会返回1m

numeral(250500).format('0.0a')会回来250.5k

numeral(10500).format('0.00a')会返回10.50k

最新更新