JavaScript中+运算符的行为来存储大量数字?
要在JavaScript中存储大量数字,请使用BigInt()而不是+运算符。如果将使用+运算符,则可能会损失精度。
假设以下是我们的大量存储,我们使用BigInt()-
console.log("Loss of precision with + operator..")示例
以下是代码-
var stringValue1="100";
console.log("The integer value=");
console.log(+stringValue1);
var stringValue2="2312123211345545367";
console.log("Loss of precision with + operator..")
console.log(+stringValue2);
const storeLongInteger=BigInt("2312123211345545367");
console.log("No loss of precision with BigInt()");
console.log(storeLongInteger);要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo212.js。
输出结果
在控制台上的输出如下-
PS C:\Users\Amit\JavaScript-code> node demo213.js The integer value= 100 Loss of precision with + operator.. 2312123211345545000 No loss of precision with BigInt()2312123211345545367n