JS精密计算代码 //加 const b2add = function(arg1, arg2) { arg1 = arg…

JS精密计算代码

//加
const b2add = function(arg1, arg2) {
	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;
}

//减
const b2sub = function(arg1, arg2) {
	return b2add(arg1, -Number(arg2), arguments[2]);
}

//乘
const b2mul = 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)));
}

//除
const b2div = function(arg1, arg2){
	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)));
}
5 条回复 A文章作者 M管理员
  1. xksh

    👍

  2. helloworld

    666

  3. 青争

    真棒👍

  4. huanglaoshi

    不错!

  5. 春哥2号

    测试评论,不错

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索