Javascript parsing int64

前端 未结 5 1218
盖世英雄少女心
盖世英雄少女心 2020-12-03 17:19

How can I convert a long integer (as a string) to a numerical format in Javascript without javascript rounding it?

var ThisInt = \'9223372036854775808\'
aler         


        
5条回答
  •  攒了一身酷
    2020-12-03 17:55

    With a little help from recursion, you can directly increment your decimal string, be it representing a 64 bit number or more...

    /**
     * Increment a decimal by 1
     *
     * @param {String} n The decimal string
     * @return The incremented value
     */
    function increment(n) {
        var lastChar = parseInt(n.charAt(n.length - 1)),
            firstPart = n.substr(0, n.length - 1);
    
        return lastChar < 9
            ? firstPart + (lastChar + 1)
            : firstPart
                ? increment(firstPart) + "0"
                : "10";
    }
    

提交回复
热议问题