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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP