首页 > 数字太大了,计算加法、减法会报错,结果不正确?怎么办?用JavaScript实现大数据(超过20位的数字)相加减运算。

数字太大了,计算加法、减法会报错,结果不正确?怎么办?用JavaScript实现大数据(超过20位的数字)相加减运算。

加法伺候 

//超过20位数值相加----------------------------------------
function bigNumAdd(a, b) {if (!(typeof a === "string" && typeof b === "string")) return console.log("传入参数必须为字符串类型");if (a.includes("+") || a.includes("-") || b.includes("+") || b.includes("-")) return console.log("传参不要夹带正负符号+、-");var aArr = a.toString().split("");var bArr = b.toString().split("");var temporary = []; //临时值//比较大小长度,aArr的值要比bArr的值要大if (bArr.length > aArr.length) {//实现两值的调换temporary = aArr;aArr = bArr;bArr = temporary;} else if (bArr.length === aArr.length) {//比较数组里面的值var flag = true; //默认aArr比bArr的值大var length = bArr.length;for (var i = 0; i < length; i++) {var numObj = parseInt(aArr[i]);var addObj = parseInt(bArr[i]);if (numObj > addObj) {break;} else if (numObj < addObj) {flag = false;break;}}if (!flag) {//实现两值的调换temporary = aArr;aArr = bArr;bArr = temporary;}}for (var i = aArr.length - 1; i >= 0; i--) { //从后面算起var numObj = parseInt(aArr[i]);var addObj = parseInt(bArr[i - (aArr.length - bArr.length)]);if (!isNaN(addObj)) {if (numObj + addObj >= 10) {aArr[i] = numObj + addObj - 10;if (i - 1 < 0) {//如果是90+10的话,超出了数组的范围aArr.splice(0, 0, 1);} else {for (var j = i - 1; j >= 0; j--) { //从后面算起var obj = parseInt(aArr[j]);if (obj + 1 === 10) {aArr[j] = 0;if (j === 0) {//循环到最后一个,将aArr数组加上最前面1// splice(position, aOfItemsToRemove, item)// 拼接函数(索引位置, 要删除元素的数量, 元素)aArr.splice(0, 0, 1);i++; //数组的长度已经改变,将循环再次加多一次break;}} else {aArr[j] = obj + 1;break;}}}} else {aArr[i] = numObj + addObj;}}}return aArr.join("");
}console.log(bigNumAdd("999999999999999999999999", "999999999999999999999998"));//1999999999999999999999997
console.log(bigNumAdd("999999999999999999999999", "99999999999999999999998"));//1099999999999999999999997
console.log(bigNumAdd("1", "999999999999999999999999"));//1000000000000000000000000
console.log(bigNumAdd("1000000000000000000000000", "1"));//1000000000000000000000001

 

减法

//超过20位数值相减----------------------------------------
function bigNumSub(a, b) {if (a === b) return "0";if (!(typeof a === "string" && typeof b === "string")) return console.log("传入参数必须为字符串类型");if (a.includes("+") || a.includes("-") || b.includes("+") || b.includes("-")) return console.log("传参不要夹带正负符号+、-");function lt(a, b) {if (a.length < b.length) {return true;} else if (a.length === b.length) {return a < b;} else {return false;}}let isMinus = false;if (lt(a, b)) {[a, b] = [b, a];isMinus = true;}let len = Math.max(a.length, b.length);a = a.padStart(len, 0);b = b.padStart(len, 0);let flag = 0,result = "",temp;for (let i = len - 1; i >= 0; i--) {temp = parseInt(a[i]) - flag - parseInt(b[i]);if (temp < 0) {result = 10 + temp + result;flag = 1;} else {result = temp + result;flag = 0;}}result = (isMinus ? "-" : "") + result.replace(/^0+/, "");return result;
}console.log(bigNumSub("999999999999999999999999", "999999999999999999999998"));//1
console.log(bigNumSub("999999999999999999999999", "99999999999999999999998"));//900000000000000000000001
console.log(bigNumSub("1", "999999999999999999999999"));//-999999999999999999999998
console.log(bigNumSub("1000000000000000000000001", "1000000000000000000000000"));//1

更多相关: