NPM酷库,每天两分钟,了解一个流行NPM库。·
JavaScript Core 并没有提供足够的数字变量格式化方式,而我们经常需要这样的操作,比如给数字加单位等,虽然需求简单,但是写起来还是要费一些周折。
numeral
numeral是一个专门用来格式化数字的NPM库,同时numeral还能解析各种格式的数字。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19const 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 支持普通数字、小数、货币、字节、百分比、时间等数字格式。
参考资料
http://numeraljs.com
https://github.com/adamwdrape...
最后
以上就是寂寞月饼最近收集整理的关于 NPM酷库:numeral,格式化数字的全部内容,更多相关内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复