简介:
得到精确的计算结果
效果展示:
调用示例:
console.log(decimalUtils.clearThousand('107,628,107,628.188'));
console.log(decimalUtils.clearThousand('107,628.188'));
console.log(decimalUtils.clearThousand(''));
console.log("-------------------------------------");
console.log(decimalUtils.formatThousand('107628107628.188'));
console.log(decimalUtils.formatThousand('107628.188'));
console.log(decimalUtils.formatThousand(''));
console.log("-------------------------------------");
console.log(decimalUtils.add(107628.188,107628.188));
console.log(decimalUtils.subtract(107628.188,107628.188));
console.log(decimalUtils.multiply(107628.188,2));
console.log(decimalUtils.divide(107628.188,107628.188));
console.log("-------------------------------------");
console.log(decimalUtils.formatThousand(decimalUtils.add(decimalUtils.clearThousand('107,628.188'),decimalUtils.clearThousand('107,628.188'))));
console.log(decimalUtils.formatThousand(decimalUtils.subtract(decimalUtils.clearThousand('107,628.188'),decimalUtils.clearThousand('107,628.188'))));
console.log(decimalUtils.formatThousand(decimalUtils.multiply(decimalUtils.clearThousand('107,628.188'),decimalUtils.clearThousand('2'))));
console.log(decimalUtils.formatThousand(decimalUtils.divide(decimalUtils.clearThousand('107,628.188'),decimalUtils.clearThousand('107,628.188'))));
代码示例:
decimalUtils = {
/**
* 函数,加法函数,用来得到精确的加法结果
* 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
* 参数:arg1:第一个加数;arg2第二个加数;
*/
add: function (arg1, arg2) {
if (null == arg1 || "" ==arg1){
arg1 = 0;
}
if (null == arg2 || "" ==arg2){
arg2 = 0;
}
arg1 = arg1.toString(), arg2 = arg2.toString();
var arg1Arr = arg1.split("."), arg2Arr = arg2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
var maxLen = Math.max(d1.length, d2.length);
var m = Math.pow(10, maxLen);
var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
var d = arguments[2];
return typeof d === "number" ? Number((result).toFixed(d)) : result;
},
/**
* 函数:减法函数,用来得到精确的减法结果
* 说明:函数返回较为精确的减法结果。
* 参数:arg1:第一个加数;arg2第二个加数;
*/
subtract: function (arg1, arg2) {
if (null == arg1 || "" ==arg1){
arg1 = 0;
}
if (null == arg2 || "" ==arg2){
arg2 = 0;
}
return decimalUtils.add(Number(arg1), -Number(arg2));
},
/**
* 函数:乘法函数,用来得到精确的乘法结果
* 说明:函数返回较为精确的乘法结果。
* 参数:arg1:第一个乘数;arg2第二个乘数;
*/
multiply: function (arg1, arg2) {
var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2];
m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
},
/**
* 函数:除法函数,用来得到精确的除法结果
* 说明:函数返回较为精确的除法结果。
* 参数:arg1:除数;arg2被除数;
*/
divide: function (arg1, arg2) {
if (null == arg1 || "" ==arg1){
arg1 = 0;
}
if (null == arg2 || "" ==arg2){
arg2 = 1;
}
var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2];
m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0);
resultVal = Number(r1.replace(".", "")) / Number(r2.replace(".", "")) * Math.pow(10, m);
return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
},
clearThousand:function(s){
if ($.trim(s) == "") {
return "0.00";
} else {
return parseFloat((s + "").replace(/[,]/g, ""));
}
},
formatThousand:function(s){
if (null == s || "" == s || "null" == s) {
return "0.00";
}
s = decimalUtils.clearThousand(s);
if (!isNaN(s) && s != "") {
s = $.trim(s + "");
var l = s.split(".")[0].split("").reverse(), r = s.indexOf(".") >= 0 ? "."
+ s.split(".")[1].substring(0, 4)
: ".00";
t = "";
for (var i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
let ret = t.split("").reverse().join("") + r;
return ret.replace('-,','-');
} else {
return '0.00';
}
}
};