srm*_*srm 4 javascript string bigint node.js
我试图将以下大型int转换为javascript中的字符串,但没有成功.我的目标最终将以"582235852866076672"结束
var foo = 582235852866076672;
console.log(foo); // 582235852866076700
var baz = "'" + 582235852866076672 + "'";
console.log(baz); // '582235852866076700'
var emptyString = 582235852866076672+'';
console.log(emptyString); // 582235852866076700
var n = foo.toString();
console.log(n); // 582235852866076700
Run Code Online (Sandbox Code Playgroud)
我认为数字太大而结果导致精度不足.我把bigint库包括在内没有成功:
var bigint = require('bigint');
var bigintLibrary = bigint(582235852866076672).toString();
console.log(bigintLibrary); //582235852866076700
Run Code Online (Sandbox Code Playgroud)
bigint库中的toSting方法指出:
"将所请求的基数中的bigint实例打印出来作为字符串."
我感谢所有的帮助和评论.谢谢.
小智 13
从 2019 年开始,您可以使用内置的 BigInt 和 BigInt 文字,首先执行以下操作:
// Cast to BigInt:
var result = BigInt("1234567801234567890");
result = BigInt(1234567801234567890);
// Or use BigInt literal directly (with n suffix).
result = 1234567801234567890n
Run Code Online (Sandbox Code Playgroud)
然后使用内置toString方法:
console.log('my BigInt is', result.toString());
Run Code Online (Sandbox Code Playgroud)
请参阅MDN 上的文档