在财务软件中,涉及金额的字段需要加千分位分隔符,accounting.js 就是这样一个工具。
1、npm安装包。npm install accounting --save
2、引入。import accounting from 'accounting';
3、通用配置。
import accounting from 'accounting';
accounting.settings = {
currency: {
symbol: '¥', // default currency symbol is '$'
format: '%s%v', // controls output: %s = symbol, %v = value/number (can be object: see below)
decimal: '.', // decimal point separator
thousand: ',', // thousands separator
precision: 2 // decimal places
},
number: {
precision: 0, // default precision on numbers is 0
thousand: ',',
decimal: '.'
}
};
4、使用。
accounting.formatMoney() // 金额处理
accounting.formatNumber() // 数字加千分位
5、使用时单独配置。
accounting.formatNumber(12345.11, 3, " "); // 12 345.110
accounting.formatMoney(12345.11, "¥", 2, ",", "."); // ¥12,345.11
accounting.toFixed(102.58, 1); //四舍五入为 102.6
accounting.unformat("¥12,345.11"); //将金额格式恢复为数字 12345.11