数字格式化的 js 库
数字格式化的 js 库
Numeral.js,是一个用于格式化数字和处理数字的 js 库。
Tip:目前 Star 有 9.2k,5年以前就没有在更新。其文档写得不很清晰,比如它提供了多语言,但如何切换成中文,怎么使用却没有说,对于某些问题(abbreviations 报错)笔者只有从源码、更新日志和 Issues 中寻找答案。
使用它
node 中通过 npm 安装即可:
PS E:\react-project> npm i numeral
Tip:也可以在浏览器中直接通过 src 的方式使用。
创建实例
通过 numeral() 创建一个实例:
var myNumeral = numeral(1000);
var value = myNumeral.value();
// value 1000
console.log('value', value)
感觉好鸡肋,但可以取消格式化。比如将 1,000 处理成 1000:
var myNumeral2 = numeral('1,000');
var value2 = myNumeral2.value();
// value2 1000
console.log('value2', value2)
格式化
这部分是我们最关心的。比如笔者的需求有:
- 将后台的字节数根据数值大小自动转为
KB、MB、GB等对应的单位 - 数字太长,显示区域有限,比如将
123456789转为123.5m或123.5百万
format()
通过 format() 可以将 1000 格式化成 1,000,将 123456789 格式化成 123,456,789。请看示例:
var number = numeral(1000).format('0,0');
// number: 1,000
console.log('number: ', number);
var number2 = numeral(123456789).format('0,0');
// number2: 123,456,789
console.log('number2: ', number2);
// 四舍五入
var number3 = numeral(1.93).format('0,0');
// number3: 2
console.log('number3: ', number3);
// 四舍五入
var number4 = numeral(1.23).format('0,0');
// number4: 1
console.log('number4: ', number4);
Tip:上面我们用了 0,0 这种格式,其他格式请直接看 numeral.js:
// node_modules\numeral\tests\numeral.js
describe('Format', function() {
it('should format to a number', function() {
var tests = [
[0, null, '0'],
[0, '0.00', '0.00'],
[null, null, '0'],
[NaN, '0.0', '0.0'],
[1.23,'0,0','1'],
[10000,'0,0.0000','10,000.0000'],
[10000.23,'0,0','10,000'],
[-10000,'0,0.0','-10,000.0'],
[10000.1234,'0.000','10000.123'],
[10000,'0[.]00','10000'],
[10000.1,'0[.]00','10000.10'],
[10000.123,'0[.]00','10000.12'],
[10000.456,'0[.]00','10000.46'],
[10000.001,'0[.]00','10000'],
[10000.45,'0[.]00[0]','10000.45'],
[10000.456,'0[.]00[0]','10000.456'],
[10000,'(0,0.0000)','10,000.0000'],
[-10000,'(0,0.0000)','(10,000.0000)'],
[-12300,'+0,0.0000','-12,300.0000'],
[1230,'+0,0','+1,230'],
[1230,'-0,0','1,230'],
[-1230,'-0,0','-1,230'],
[-1230.4,'0,0.0+','1,230.4-'],
[-1230.4,'0,0.0-','1,230.4-'],
[1230.4,'0,0.0-','1,230.4'],
[100.78, '0', '101'],
[100.28, '0', '100'],
[1.932,'0.0','1.9'],
[1.9687,'0','2'],
[1.9687,'0.0','2.0'],
[-0.23,'.00','-.23'],
[-0.23,'(.00)','(.23)'],
[0.23,'0.00000','0.23000'],
[0.67,'0.0[0000]','0.67'],
[3162.63,'0.0[00000000000000]','3162.63'],
[1.99,'0.[0]','2'],
[1.0501,'0.00[0]','1.05'],
[1.005,'0.00','1.01'],
// leading zero
[0, '00.0', '00.0'],
[0.23, '000.[00]', '000.23'],
[4, '000', '004'],
[10, '00000', '00010'],
[1000, '000,0', '1,000'],
[1000, '00000,0', '01,000'],
[1000, '0000000,0', '0,001,000'],
// abbreviations
[2000000000,'0.0a','2.0b'],
[1230974,'0.0a','1.2m'],
[1460,'0a','1k'],
[-104000,'0 a','-104 k'],
[999950,'0.0a','1.0m'],
[999999999,'0a','1b'],
// forced abbreviations
[-5444333222111, '0,0 ak', '-5,444,333,222 k'],
[5444333222111, '0,0 am', '5,444,333 m'],
[-5444333222111, '0,0 ab', '-5,444 b'],
[-5444333222111, '0,0 at', '-5 t'],
[123456, '0.0[0] ak', '123.46 k'],
[150,'0.0 ak','0.2 k']
],
i,
n,
output;
for (i = 0; i < tests.length; i++) {
n = numeral(tests[i][0]);
output = n.format(tests[i][1]);
expect(output).to.equal(tests[i][2]);
expect(typeof output).to.equal('string');
}
});
});
字节转换
比如将 1024 转为 1KB,将 1024*1024 转为 1MB。将请看示例:
PS D:\spug-study> node
Welcome to Node.js v16.14.2.
Type ".help" for more information.
> let numeral = require('numeral')
undefined
> numeral(1024).format('0b')
'1KB'
> numeral(1024*1024).format('0b')
'1MB'
> numeral(1024*1024*1024).format('0b')
'1GB'
> numeral(1024*1024*1024*1024).format('0b')
'1TB'
> numeral(1024*1024*1024*1024*32).format('0b')
'35TB'
>
Tip:笔者直接在 node 环境下运行。更多格式化语法请看 bytes.js 文件。
// node_modules\numeral\tests\formats\bytes.js
it('should format to bytes', function() {
var decimal = 1000;
var binary = 1024;
var tests = [
[0,'0b','0B'],
[null,'0 b','0 B'],
[100,'0b','100B'],
[binary * 2,'0 ib','2 KiB'],
[Math.pow(binary, 2) * 5,'0ib','5MiB'],
[Math.pow(binary, 3) * 7.343,'0.[0] ib','7.3 GiB'],
[Math.pow(binary, 4) * 3.1536544,'0.000ib','3.154TiB'],
[Math.pow(binary, 5) * 2.953454534534,'0ib','3PiB'],
[decimal * 2,'0 b','2 KB'],
[Math.pow(decimal, 2) * 5,'0b','5MB'],
[Math.pow(decimal, 3) * 7.343,'0.[0] b','7.3 GB'],
[Math.pow(decimal, 4) * 3.1536544,'0.000b','3.154TB'],
[Math.pow(decimal, 5) * 2.953454534534,'0b','3PB']
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
}
});
字节转数字
比如可以将 1KB 转为数字,但结果只是 1000,如果要 1024,需要使用 1 KiB。请看示例:
> numeral('1KB').value()
1000
> numeral('1 KiB').value()
1024
> numeral('1MB').value()
1000000
Tip:有关字节反解析的更多介绍请看 bytes.js。
// node_modules\numeral\tests\formats\bytes.js
it('should unformat to number', function() {
var decimal = 1000;
var binary = 1024;
var tests = [
['0B', 0],
['0 B', 0],
['100B', 100],
['2 KiB', binary * 2],
['5MiB', Math.pow(binary, 2) * 5],
['7.3 GiB', Math.pow(binary, 3) * 7.3],
['3.154TiB', Math.pow(binary, 4) * 3.154],
['3PiB', Math.pow(binary, 5) * 3],
['2 KB', decimal * 2],
['5MB', Math.pow(decimal, 2) * 5],
['7.3 GB', Math.pow(decimal, 3) * 7.3],
['3.154TB', Math.pow(decimal, 4) * 3.154],
['3PB', Math.pow(decimal, 5) * 3]
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
}
});
时间转换转化
将数字(秒)转为时间形式。请看示例:
> numeral(1).format('00:00:00')
'0:00:01'
> numeral(60).format('00:00:00')
'0:01:00'
> numeral(60*60).format('00:00:00')
'1:00:00'
百分比转化
请看示例:
> numeral(0.974878234).format('0.000%')
'97.488%'
> numeral(0).format('0%')
'0%'
> numeral(1).format('0%')
'100%'
Tip:更多介绍请看 node_modules\numeral\tests\formats\percentage.js。
货币转化
请看示例:
> numeral(1000.234).format('$0,0.00')
'$1,000.23'
Tip:更多用法请看 currency.js。
// node_modules\numeral\tests\formats\currency.js
describe('Currency', function() {
after(function() {
numeral.reset();
});
it('should format to currency', function() {
var tests = [
[0,'$0.00','$0.00'],
[null,'$0.00','$0.00'],
[0.99,'$0,0.00','$0.99'],
[1000.234,'$0,0.00','$1,000.23'],
[1001,'$ 0,0.[00]','$ 1,001'],
[1000.234,'0,0.00 $','1,000.23 $'],
[-1000.234,'0,0.00 $','-1,000.23 $'],
[-1000.234,'($0,0)','($1,000)'],
[-1000.234,'(0,0$)','(1,000$)'],
[-1000.234,'(0,0 $)','(1,000 $)'],
[-1000.234,'$0.00','-$1000.23'],
[-1000.234,'$ 0.00','-$ 1000.23'],
[1230974,'($0.00 a)','$1.23 m'],
[-1000.234,'$ (0,0)','$ (1,000)'],
[-1000.234,'$(0,0)','$(1,000)'],
[-1000.234,'$ (0,0.00)','$ (1,000.23)'],
[-1000.234,'$(0,0.00)','$(1,000.23)'],
[-1000.238,'$(0,0.00)','$(1,000.24)'],
[-1000.234,'$-0,0','$-1,000'],
[-1000.234,'$ -0,0','$ -1,000'],
[1000.234,'$ (0,0)','$ 1,000'],
[1000.234,'$(0,0)','$1,000'],
[1000.234,'$ (0,0.00)','$ 1,000.23'],
[1000.234,'$(0,0.00)','$1,000.23'],
[1000.238,'$(0,0.00)','$1,000.24'],
[1000.234,'$-0,0','$1,000'],
[1000.234,'$ -0,0','$ 1,000']
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
}
});
it('should unformat to currency', function() {
var tests = [
['$0.00', 0],
['$0.99', 0.99],
['$1,000.23', 1000.23],
['1,000.23 $', 1000.23],
['($1,000)', -1000],
['-1,000$', -1000],
['$1.23 m', 1230000],
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
}
});
});
指数转化
请看示例:
> numeral(77777777.1234).format('0.0e+0')
'7.8e+7'
Tip:更多用法请看 exponential.js。
// node_modules\numeral\tests\formats\exponential.js
it('should format to exponential notation', function() {
var tests = [
[0,'0e+0','0e+0'],
[null,'0e+0','0e+0'],
[1,'0e+0','1e+0'],
[77.1234,'0.0e+0','7.7e+1'],
[0.000000771234,'0.0e-0','7.7e-7'],
[-0.000000771234,'0.00e-0','-7.71e-7'],
[77.1234,'0.000e+0','7.712e+1'],
[-1000830298,'0.0[000]e+0','-1.0008e+9']
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
}
});
比特率转化
比特率,是指单位时间内传送的比特(bit)数,单位为bps(bit per second)。
> numeral(.0056).format('0 BPS')
'56 BPS'
Tip:更多用法请看 bps.js:
// node_modules\numeral\tests\formats\bps.js
it('should format to bps', function() {
var tests = [
[0,'0 BPS','0 BPS'],
[0.0001, '0 BPS', '1 BPS'],
[.0056, '0 BPS', '56 BPS'],
[.25, '0BPS', '2500BPS'],
[.000001, '0.00 BPS', '0.01 BPS']
],
i;
for (i = 0; i < tests.length; i++) {
expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
}
});
四则运算
numeral 提供加减乘除四则运算的方法,例如 1000 加 10 等于 1010。请看示例:
> numeral(1000).add(10).value()
1010
Tip:更多介绍请看 numeral.js。
// node_modules\numeral\tests\numeral.js
describe('Manipulate', function() {
// 加法
describe('Add', function() {
it('should add', function() {
var tests = [
[1000,10,1010],
[0.5,3,3.5],
[-100,200,100],
[0.1,0.2,0.3],
[0.28,0.01,0.29],
[0.289999,0.000001,0.29],
[0.29,0.01,0.3]
],
num;
for (var i = 0; i < tests.length; i++) {
num = numeral(tests[i][0]);
num.add(tests[i][1]);
expect(num.value()).to.equal(tests[i][2]);
}
});
});
// 减法
describe('Subtract', function() {
it('should subtract', function() {
var tests = [
[1000,10,990],
[0.5,3,-2.5],
[-100,200,-300],
[0.3,0.1,0.2],
[0.28,0.01,0.27],
[0.29,0.01,0.28]
],
num;
for (var i = 0; i < tests.length; i++) {
num = numeral(tests[i][0]);
num.subtract(tests[i][1]);
expect(num.value()).to.equal(tests[i][2]);
}
});
});
// 乘法
describe('Multiply', function() {
it('should multiply', function() {
var tests = [
[1000,10,10000],
[0.5,3,1.5],
[-100,200,-20000],
[0.1,0.2,0.02],
[0.28,0.01,0.0028],
[0.29,0.01,0.0029],
[0.00000231,10000000,23.1]
],
num;
for (var i = 0; i < tests.length; i++) {
num = numeral(tests[i][0]);
num.multiply(tests[i][1]);
expect(num.value()).to.equal(tests[i][2]);
}
});
});
// 除法
describe('Divide', function() {
it('should divide', function() {
var tests = [
[1000,10,100],
[0.5,3,0.16666666666666666],
[-100,200,-0.5],
[5.3,0.1,53],
[0.28,0.01,28],
[0.29,0.01,29]
],
num;
for (var i = 0; i < tests.length; i++) {
num = numeral(tests[i][0]);
num.divide(tests[i][1]);
expect(num.value()).to.equal(tests[i][2]);
}
});
});
// 差值。例如 1000 和 10 相差 990。
describe('Difference', function() {
it('should find a difference', function() {
var tests = [
[1000,10,990],
[0.5,3,2.5],
[-100,200,300],
[0.3,0.2,0.1],
[0.28,0.01,0.27],
[0.29,0.01,0.28]
],
num;
for (var i = 0; i < tests.length; i++) {
num = numeral(tests[i][0]);
expect(num.difference(tests[i][1])).to.equal(tests[i][2]);
}
});
});
// 四舍五入。
describe('Rounding', function() {
it('should format with rounding', function() {
var tests = [
// value, format string, expected w/ floor, expected w/ ceil
[2280002, '0.00a', '2.28m', '2.29m'],
[10000.23,'0,0','10,000', '10,001'],
[1000.234,'0,0.00','1,000.23', '1,000.24'],
[0.97487823,'0.000','0.974','0.975'],
[-0.433,'0.0','-0.5', '-0.4']
],
i;
for (i = 0; i < tests.length; i++) {
// floor
expect(numeral(tests[i][0]).format(tests[i][1], Math.floor)).to.equal(tests[i][2]);
// ceil
expect(numeral(tests[i][0]).format(tests[i][1], Math.ceil)).to.equal(tests[i][3]);
}
});
});
});
多语言
需求:数字很长时,由于排版的考虑,需要将数字显示的更短,转为指数勉强可以,但对很多人还是不友好,如果能转为 123.5百万 这种形式就完美了。
numeral 提供了缩写,比如可以将 1230974 转为 1.2m(m,指百万):
// node_modules\numeral\tests\numeral.js
...
// abbreviations
[2000000000,'0.0a','2.0b'],
[1230974,'0.0a','1.2m'],
[1460,'0a','1k'],
[-104000,'0 a','-104 k'],
[999950,'0.0a','1.0m'],
[999999999,'0a','1b'],
请看示例:
const numeral = require('numeral');
var number5 = numeral(123456789).format('0.0a')
// 123.5m
console.log('number5: ', number5);
123.5m 是多少,能否转为中文?笔者在 chs.js 中找到如下代码:
// node_modules\numeral\locales\chs.js
numeral.register('locale', 'chs', {
delimiters: {
thousands: ',',
decimal: '.'
},
abbreviations: {
thousand: '千',
million: '百万',
billion: '十亿',
trillion: '兆'
},
ordinal: function (number) {
return '.';
},
currency: {
symbol: '¥'
}
});
通过引入 chs 并切换成中文,成功将 123456789 转为 123.5百万。请看示例:
const numeral = require('numeral');
// 加载中文
+ require('numeral/locales/chs')
// 切换成中文
+ numeral.locale('chs')
const number5 = numeral(123456789).format('0.0a')
// number5: 123.5百万
console.log('number5: ', number5);
Tip:对于单页面项目,在其他组件中若需要使用英文,比如不要 123.5百万,而要 123.5m,直接切换语言(numeral.locale('en'))即可。就像这样:
...
const number5 = numeral(123456789).format('0.0a')
// number5: 123.5百万
console.log('number5: ', number5);
+ numeral.locale('en')
const number6 = numeral(123456789).format('0.0a')
// number6: 123.5m
console.log('number6: ', number6);
笔者的示例
import React from 'react';
const numeral = require('numeral');
require('numeral/locales/chs')
numeral.locale('chs')
function Test() {
const number = numeral(1000).format('0,0');
// number: 1,000
console.log('number: ', number);
const number2 = numeral(123456789).format('0,0');
// number2: 123,456,789
console.log('number2: ', number2);
// 四舍五入
const number3 = numeral(1.93).format('0,0');
// number3: 2
console.log('number3: ', number3);
// 四舍五入
const number4 = numeral(1.23).format('0,0');
// number4: 1
console.log('number4: ', number4);
const number5 = numeral(123456789).format('0.0a')
// number5: 123.5百万
console.log('number5: ', number5);
numeral.locale('en')
const number6 = numeral(123456789).format('0.0a')
// number6: 123.5m
console.log('number6: ', number6);
return (
<div>
react 项目
</div>
)
}
export default Test
数字格式化的 js 库的更多相关文章
- Numeral.js 是一个用于格式化和数字四则运算的js 库
1.Numeral.js 是一个用于格式化和数字四则运算的js 库. 2.支持多种语言,包含中文在内的17种语言. 在浏览器中引用js文件: <script src="numeral. ...
- js 金融数字格式化
js 金融数字格式化 finance money number format 数字格式化 regex `123456789`.replace(/\B(?=(\d{3})+(?!\d))/g, ',') ...
- js数字格式化-四舍五入精简版
搜索网上的,数字格式化过余复杂,自己想了个简单方法,欢迎吐槽. 简化说明: '123333' => 12.3万 parseInt('123333') 字符串转整型 parseInt('12333 ...
- Vue-admin工作整理(十九):从数字渐变组件谈第三方JS库Count-to的使用
1.组件封装基础 npm install countup@latest 2.组件中使用id值 3.组件中获得dom 如何封装一个组件,在组件中用到需要传入HTML元素ID值的JS库时如何处理,如何获取 ...
- js数字格式化(截断格式化或四舍五入格式化)
/*** * 数字格式化(适合金融产品截断小数位后展示) * @param num * @param pattern (标准格式:#,###.## 或#.## 或#,###00.00) * @para ...
- 主流JS库一览
主流JS库一览 标签: prototypedojomootoolsprototypejsjqueryjavascript 2009-10-14 22:52 19936人阅读 评论(2) 收藏 举报 ...
- 12个值得关注的顶级可视化JS库 涉及图表、动画、时间处理,表格操作
本文是译文,原文是https://da-14.com/blog/top-11...我在原文的基础上加了百度的Echats图表库,这个也是毫不逊色其他图表库的.另外Handsontable电子表格库也是 ...
- 一些JS库汇总
作者:wlove 链接:https://www.zhihu.com/question/429436558/answer/2348777302 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权, ...
- 近期写js库中遇到的一个判别的问题
最近在写一个自己的js库,正写到数组包,在里面定义了一个排序,只对纯数字数据进行排序的方法,但是在测试的时候发现一个很诡异的问题,那就是传入一个对象的时候,它没有返回erroemsg而是返回了对象,上 ...
随机推荐
- GRPC-go版本
GRPC-go版本 1.安装GO,protobuf 只适合有梯子的 GO的安装没必要说了 protobuf :https://github.com/protocolbuffers/protobuf/r ...
- vue预渲染及其cdn配置
VUE SEO方案一 - 预渲染及其cdn配置 项目接入VUE这样的框架后,看起来真是太漂亮了,奈何与MCV框架比起来,单页应用程序却满足不了SEO的业务需求,首屏渲染时间也是个问题.总不能白学VUE ...
- 『忘了再学』Shell基础 — 12、用户自定义变量
目录 1.变量定义 2.变量调用 3.变量的修改 4.查看变量 (1)-u选项说明: (2)-x选项说明: (3)set命令说明: 5.变量删除 1.变量定义 变量正确定义. 格式:变量名=" ...
- Python图像处理丨OpenCV+Numpy库读取与修改像素
摘要:本篇文章主要讲解 OpenCV+Numpy 图像处理基础知识,包括读取像素和修改像素. 本文分享自华为云社区<[Python图像处理] 二.OpenCV+Numpy库读取与修改像素> ...
- CV技术指南免费版知识星球
最近公众号的交流群满了,我们决定搞一个免费的知识星球,让大家在里面交流.以往都是我们写原创,大家阅读,读者之间没什么交流.与此同时,在CV技术指南交流群里,大部分问题都得到了很好地解决,但从来没有 ...
- 华为麒麟团队力造的Python,整整26G
华为团队力造的Python,下面链接自取 https://docs.qq.com/doc/DRkZvRkxvb292c2Vz
- -2.输入加速(cin,cout)
+ ios::sync_with_stdio(false);//加速几百毫秒 cin.tie(0); // 接近scanf cout.tie(0);
- 9.2 Linux硬盘分区和挂载
一块新的硬盘存储设备后,先需要分区,然后再格式化文件系统,最后才能挂载并正常使用. 分区:根据需求和硬盘大小划分空间 格式化:对分区安装文件系统 挂载:将设备文件与一个目录关联的动作叫挂载 硬盘分区格 ...
- STS快捷键
在类或者方法上方加注释:shift+alt+J
- 1.4 类UNIX系统是什么鬼?
上节<UNIX和Linux的区别>中讲到了 UNIX 系统的历史,UNIX 是操作系统的开山鼻祖,是操作系统的发源地,后来的 Windows 和 Linux 都参考了 UNIX. 有人说, ...