NPM酷库,每天两分钟,了解一个流行的NPM库。·上次,我们学习了如何使用数字库格式化数字。今天我们继续了解另一个负责格式化数字的库。accountingaccounting提供的主要方法有formatMoney()formatColumn()formatNumber()toFixed()unformat()下面一一介绍:formatMoney()formatcurrency//默认货币格式:货币符号$,保留两位小数,每千加逗号accounting.formatMoney(12345678);//$12,345,678.00//指定货币符号,保留小数位,千位空格accounting.formatMoney(12345678,'¥',2,'');//¥12345678.00formatColumn()格式化并按列对齐。在制表的时候,formatColumn()方法方便我们按照表格列对齐数字和货币符号:accounting.formatColumn([123.5,3456.615,777888.99,-5432,-1234567,0],"$");格式化后的效果:formatNumber()格式化数字accounting.formatNumber(5318008);//5,318,008accounting.formatNumber(9876543.21,3,"");//9876543.210toFixed()保留小数位和JavaScriptBuilt-inNumber.prototype.toFixed()不同的是accounting.toFixed()有一个舍入效果:(0.615).toFixed(2);//"0.61"accounting.toFixed(0.615,2);//"0.62"unformat()解析数字unformat()方法可以从任何格式的字符串中解析出原始数字:accounting.unformat("£12,345,678.90GBP");//12345678.9参考http://openexchangerates.github...https://github.com/openexchan...
