JavaScript大位数相加减
function arrayAdd(number, addNumber) { var numberArr = number.toString().split(''); var addNumberArr = addNumber.toString().split(''); var temporary = []; //临时值 //比较大小长度,numberArr的值要比addNumberArr的值要大 if (addNumberArr.length > numberArr.length) { //实现两值的调换 temporary = numberArr; numberArr = addNumberArr; addNumberArr = temporary; } else if (addNumberArr.length === numberArr.length) { //比较数组里面的值 var flag = true; //默认numberArr比addNumberArr的值大 var length = addNumberArr.length; for (var i = 0; i < length; i++) { var numObj = parseInt(numberArr[i]); var addObj = parseInt(addNumberArr[i]); if (numObj > addObj) { break; } else if (numObj < addObj) { flag = false; break; } } if (!flag) { //实现两值的调换 temporary = numberArr; numberArr = addNumberArr; addNumberArr = temporary; } } for (var i = numberArr.length - 1; i >= 0; i--) { //从后面算起 var numObj = parseInt(numberArr[i]); var addObj = parseInt(addNumberArr[i - (numberArr.length - addNumberArr.length)]); if (!isNaN(addObj)) { if (numObj + addObj >= 10) { numberArr[i] = numObj + addObj - 10; if (i - 1 < 0) { //如果是90+10的话,超出了数组的范围 numberArr.splice(0, 0, 1); } else { for (var j = i - 1; j >= 0; j--) { //从后面算起 var obj = parseInt(numberArr[j]); if (obj + 1 === 10) { numberArr[j] = 0; if (j === 0) { //循环到最后一个,将numberArr数组加上最前面1 // splice(position, numberOfItemsToRemove, item) // 拼接函数(索引位置, 要删除元素的数量, 元素) numberArr.splice(0, 0, 1); i++; //数组的长度已经改变,将循环再次加多一次 break; } } else { numberArr[j] = obj + 1; break; } } } } else { numberArr[i] = numObj + addObj; } } } return numberArr.join(""); } //判断大小,返回true或者false function arrayCompare(firstNumber, sendNumber) { var firstNumberArr = firstNumber.toString().split(''); var sendNumberArr = sendNumber.toString().split(''); //比较大小长度,firstNumberArr的值要比sendNumberArr的值要大 if (firstNumberArr.length > sendNumber.length) { return true; } else if (sendNumberArr.length > firstNumberArr.length) { //实现两值的调换 return false; } else if (sendNumberArr.length === firstNumberArr.length) { //比较数组里面的值 var flag = true; //默认firstNumberArr比sendNumberArr的值大 var length = sendNumberArr.length; for (var i = 0; i < length; i++) { var numObj = parseInt(firstNumberArr[i]); var addObj = parseInt(sendNumberArr[i]); if (numObj > addObj) { break; } else if (numObj < addObj) { flag = false; break; } } if (!flag) { //实现两值的调换 return false; } else { return true; } } } //startNumber是减数,endNumber是正减除数 function arrayDiff(startNumber, endNumber) { var startNumberArr = startNumber.toString().split(''); var endNumberArr = endNumber.toString().split(''); var temporary = []; //临时值 var positiveNumber = true;//如果startNumber大于endNumber的话,就返回负值 //比较大小长度,endNumberArr 的值要比startNumberArr 的值要大 if (startNumberArr.length > endNumberArr.length) { //实现两值的调换 temporary = endNumberArr; endNumberArr = startNumberArr; startNumberArr = temporary; positiveNumber = false; } else if (endNumberArr.length === startNumberArr.length) { //比较数组里面的值 var flag = true; //默认endNumberArr比startNumberArr的值大 var length = startNumberArr.length; for (var i = 0; i < length; i++) { var endObj = parseInt(endNumberArr[i]); var startObj = parseInt(startNumberArr[i]); if (endObj > startObj) { break; } else if (endObj < startObj) { flag = false; break; } } if (flag === false) { //实现两值的调换 temporary = endNumberArr; endNumberArr = startNumberArr; startNumberArr = temporary; positiveNumber = false; } } for (var i = endNumberArr.length - 1; i >= 0; i--) { //从后面算起 var endObj = parseInt(endNumberArr[i]); var startObj = parseInt(startNumberArr[i - (endNumberArr.length - startNumberArr.length)]); if (!isNaN(startObj)) { if (endObj - startObj < 0) { endNumberArr[i] = endObj + 10 - startObj; for (var j = i - 1; j >= 0; j--) { //从后面算起 var obj = parseInt(endNumberArr[j]); if (obj === 0) { endNumberArr[j] = 9; } else { endNumberArr[j] = obj - 1; break; } } } else { endNumberArr[i] = endObj - startObj; } } } //判断前面是否为0,例如012,从前面算起 for (var i = 0; i < endNumberArr.length; i++) { var obj = endNumberArr[i]; if (obj !== null) { if (obj === 0) { endNumberArr.splice(i, 1); i--; } else { break; } } } var result = endNumberArr.join(""); if (!positiveNumber) { result = "-" + result; } return result; } //传进去的时候必须是字符串,不然会被计算机默认为number类型,从而数字发生变化 var b = arrayDiff("21111111111111118", "21111111111111119"); //alert(b);
我们度尽的年岁,好像一声叹息,不过是劳苦愁烦,转眼成空