以分为单位的金额格式化
/**
* @description: 格式化金额
*/
'use strict';
var symbol = '¥';
class Amount{
/**
* 格式化金额
* @param {number} amount 金额 单位(分)
* @return xx.00 格式字符串
*/
formatAmount(amount){
return this.formatAmountWithAutoLength(amount, 'normal', 2);
}
/**
* 格式化金额
* @param {number} amount 金额 单位(分)
* @param {string} lengthLevel 显示位数处理 , 见 formatAmountWithAutoLength
* @param {number} decimalPlace 小数位 默认2位
* @return xx.00 or xx 格式字符串,小数位不为0时返回xx.xx, 为0时返回xx
*/
formatAmountWithAutoDecimal(amount, lengthLevel = 'normal', decimalPlace = 2){
if (amount % 100 > 0) {
return this.formatAmountWithAutoLength(amount, lengthLevel, decimalPlace);
} else {
return this.formatAmountWithAutoLength(amount, lengthLevel, 0);
}
}
/**
* 格式化金额
* @param {number} amount 金额 单位(分)
* @param {string} lengthLevel 显示位数处理
* 'normal' 不做位数处理
* 'oneHundredThousand' 最多显示十万级别
* 'oneHundredMillion' 最多显示亿元级别
* @param {string} decimalPlace 是否显示小数
* @return {string}
*/
formatAmountWithAutoLength(amount, lengthLevel = 'auto', decimalPlace = 2){
var low = amount < 0;
if (low) {
amount = 0 - amount;
}
var unit = "";
if( lengthLevel === 'oneHundredMillion' ) {
if (amount >= 100 * 10000 * 10000) {
amount /= 100000000;
// 亿级别
unit = "亿";
}
} else if ( lengthLevel === 'oneHundredThousand' ) {
// 10万级别
if (amount >= 1000000 * 100) {
amount /= 10000;
unit = "万";
}
} else if ( lengthLevel === 'tenThousand' ) {
// 1万级别
if (amount >= 10000 * 100) {
amount /= 10000;
unit = "万";
}
} else if ( lengthLevel === 'auto' ) {
if (amount >= 100 * 10000 * 10000) {
amount /= 100000000;
// 亿级别
unit = "亿";
}
// 10万级别
else if (amount >= 1000000 * 100) {
amount /= 10000;
unit = "万";
}
}
var str;
decimalPlace = decimalPlace >= 0 && decimalPlace <= 20 ? decimalPlace : 2;
str = (amount / 100).toFixed(decimalPlace) + "";
var l = str.split(".")[0].split("").reverse(), r = str.split(".")[1];
var temp = "";
for (var i = 0; i < l.length; i++) {
temp += l[i] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? "," : "");
}
if (r) {
str = temp.split("").reverse().join("") + "." + r;
} else {
str = temp.split("").reverse().join("");
}
if (low) {
str = '-' + str;
}
return str + unit;
}
/**
* 格式化金额, 带币种
* @param {number} amount 金额 单位(分)
* @return xx.00 格式字符串
*/
formatSymbolAmount(amount){
return symbol + this.formatAmount(amount);
}
/**
* 格式化金额, 带币种
* @param {number} amount 金额 单位(分)
* @param {string} lengthLevel 显示位数处理 , 见 formatAmountWithAutoLength
* @param {number} decimalPlace 小数位 默认2位
* @return xx.00 or xx 格式字符串,小数位不为0时返回xx.xx, 为0时返回xx
*/
formatSymbolAmountWithAutoDecimal(amount, lengthLevel = 'normal', decimalPlace = 2){
return symbol + this.formatAmountWithAutoDecimal(amount, lengthLevel, decimalPlace);
}
/**
* 格式化金额
* @param {number} amount 金额 单位(分)
* @param {string} lengthLevel 显示位数处理
* 'normal' 不做位数处理
* 'oneHundredThousand' 最多显示十万级别
* 'oneHundredMillion' 最多显示亿元级别
* @param {string} decimalPlace 是否显示小数
* @return {string}
*/
formatSymbolAmountWithAutoLength(amount, lengthLevel = 'auto', decimalPlace = 2){
return symbol + this.formatAmountWithAutoLength(amount, lengthLevel, decimalPlace);
}
/**
* 4位小数
* @param {[type]} amount [description]
* @return {[type]} [description]
*/
formatAmountForReport(amount){
var low = amount < 0;
if (low) {
amount = 0 - amount;
}
var unit = "";
if (amount >= 100 * 10000 * 10000) {
amount /= 100000000;
// 亿级别
unit = "亿";
}
// 1万级别
else if (amount >= 10000 * 100) {
amount /= 10000;
unit = "万";
}
var str;
var decimalPlace = 0;
if (amount < 1000) {
decimalPlace = 3;
amount = parseInt(amount * 10, 10) / 10;
} else if (amount < 10000) {
decimalPlace = 2;
amount = parseInt(amount, 10);
} else if (amount < 100000) {
decimalPlace = 1;
amount = parseInt(amount / 10, 10) * 10;
} else {
amount = parseInt(amount / 100, 10) * 100;
}
str = (amount / 100).toFixed(decimalPlace) + "";
if (low) {
str = '-' + str;
}
return str + unit;
}
add(arg1, arg2) {
var r1, r2, m;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (error) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (error) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
return (arg1 * m + arg2 * m) / m;
}
sub(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (error) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (error) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
// 动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
div(arg1, arg2) {
var t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
} catch (error) {
//
}
try {
t2 = arg2.toString().split(".")[1].length;
} catch (error) {
//
}
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return this.mul((r1 / r2), Math.pow(10, t2 - t1));
}
mul(arg1, arg2) {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length;
} catch (error) {
//
}
try {
m += s2.split(".")[1].length;
} catch (error) {
//
}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
}
module.exports = new Amount();