Home > Web Front-end > JS Tutorial > Fun JavaScript question: Adding large integer strings

Fun JavaScript question: Adding large integer strings

黄舟
Release: 2017-01-22 14:49:31
Original
2077 people have browsed it

Many languages ​​provide native support for adding two large integer strings.

For example, Java provides the BigInteger class, but JS does not support this aspect, so we have to rely on ourselves to implement it.

The following string addition function receives two string parameters and returns the result after their addition, which is also in string form.

The main idea is to add bit by bit and carry. There are still many details to consider during implementation.

function sumStrings(a,b) {  
    //通过补零让a和b对齐  
    //若a比b短,则对a补零  
    while(a.length < b.length){  
        a = "0" + a;  
    }  
    //若b比a短,则对b补零  
    while(b.length < a.length){  
        b = "0" + b;  
    }  
    //是否有进位  
    var addOne = 0;  
    //结果数组  
    var result = [];  
    //从个位开始相加  
    for(var i=a.length-1;i>=0;i--){  
        var c1 = a.charAt(i) - 0;  
        var c2 = b.charAt(i) - 0;  
        var sum = c1 + c2 + addOne;  
        //若数字相加大于9,则进位  
        if(sum > 9){  
            result.unshift(sum - 10);  
            addOne = 1;  
        }  
        else{  
            result.unshift(sum);  
            addOne = 0;  
        }  
    }  
    //应付下面的情况:  
    //"99" + "11" => "110"  
    //它最后仍然要进位  
    if(addOne){  
        result.unshift(addOne);  
    }  
    //应付如下的情况  
    //"01" + "01" => "2"  
    //而不是"02",所以移除第一位的"0"  
    if(!result[0]){  
        result.splice(0,1);  
    }  
    return result.join("");  
}
Copy after login

The above is the interesting JavaScript question: the addition of large integer strings. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template