joj*_*nya 3 javascript arrays numbers
每当我运行它时,返回的数字都会增加,谁能向我解释一下?
let array = [9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 1]
return Number(array.join(''))
Run Code Online (Sandbox Code Playgroud)
输出:
9223372036854772
Run Code Online (Sandbox Code Playgroud)
该数字大于Number.MAX_SAFE_INTEGER(2 53 -1)。您可能想BigInt改用。
例子:
let array = [9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 1]
let num = BigInt(array.join(''));
console.log(num.toString());
console.log("Doubled:", (num * 2n).toString());
console.log("Squared:", (num ** 2n).toString());Run Code Online (Sandbox Code Playgroud)
您可以使用它Number.isSafeInteger来检查值是否精确。
let array = [9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 1]
let num = Number(array.join(''));
console.log("Safe?", Number.isSafeInteger(num));Run Code Online (Sandbox Code Playgroud)