NPM酷库:numeral,格式化数字

NPM酷库,天天两分钟,了解一个流行NPM库。·git

JavaScript Core 并无提供足够的数字变量格式化方式,而咱们常常须要这样的操做,好比给数字加单位等,虽然需求简单,可是写起来仍是要费一些周折。github

numeral

numeral是一个专门用来格式化数字的NPM库,同时numeral还能解析各类格式的数字。ui

const numeral = require('numeral');

// 解析数字
numeral('10,000.12'); // 10000.12
numeral('$10,000.00'); // 10000
numeral('3.467TB'); // 3467000000000
numeral('-76%'); // -0.76

// 格式化
numeral(10000.23).format('0,0'); // '10,000'
numeral(10000.1234).format('0.000'); // '10000.123'
numeral(100.1234).format('00000'); // '00100'
numeral(1230974).format('0.0a'); // '1.2m'
numeral(100).format('0o'); // '100th'
numeral(1000.234).format('$0,0.00'); // '$1,000.23'
numeral(7884486213).format('0.00b'); // '7.88GB'
numeral(0.974878234).format('0.000%'); // '97.488%'
numeral(238).format('00:00:00'); // '17:44:06'

numeral 支持普通数字、小数、货币、字节、百分比、时间等数字格式。code

参考资料

http://numeraljs.comorm

https://github.com/adamwdrape...ip

相关文章
相关标签/搜索