使用 numeric.js 格式化大数

use*_*686 3 format numeral.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
Run Code Online (Sandbox Code Playgroud)

Yco*_*con 5

Numeral.js 内置了这个。它可以很容易地使用a诸如.format('0.00a').

一些完整的例子:

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

numeral(250500).format('0.0a') 将返回 250.5k

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