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
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短