Javascript 将 bigInt 转换为字符串

Javascript convert bigInt to string

我试图将以下大整数转换为 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

我觉得这个数字太大了,结果失去了精度。 我包含了 bigint library 但没有成功:

var bigint = require('bigint');
var bigintLibrary = bigint(582235852866076672).toString();
console.log(bigintLibrary); //582235852866076700

bigint 库中的 toSting 方法指出:

"Print out the bigint instance in the requested base as a string."

感谢所有的帮助和评论。谢谢。

这是一个精度问题——您返回的数字 (582235852866076672) 大于 JavaScript 中可表示的最大数字,即 2^53 或 9007199254740992.

您可以使用 BigInt 方法 BigInt.prototype.toLocaleString()BigInt.prototype.toString()。希望有所帮助。

单击此处:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt 了解更多 BigInt 信息。

自 2019 年起,您可以使用内置的 BigInt 和 BigInt 文字,首先,执行如下操作:

// Cast to BigInt:
var result = BigInt("1234567801234567890");
result = BigInt(1234567801234567890);

// Or use BigInt literal directly (with n suffix).
result = 1234567801234567890n

然后使用内置的toString方法:

console.log('my BigInt is', result.toString());

documentation on MDN

似乎即使有三个答案,但实际上只有一个回答了问题,但这不是公认的答案...


这是我的回答:

首先,你不需要导入BigInt库...BigInt内置于JavaScript(至少现在是),你可以通过调用函数来访问它: BigInt(...) 或在数字末尾添加 'n':45625625443n

BigInt 可以容纳非常大的数字,上次我检查过,限制是 10 亿位 1。这意味着你可以持有一个大约 1×10109 的整数,在大多数情况下你很可能不需要那么大的整数。

如上所示,您可以使用构造函数 (BigInt(...)) 或在末尾添加 'n' (45625625443n) 来构建 BigInt 2

使用构造函数时,参数可以是以下任何一个(但正如jmrk在评论中所说,在构造函数中输入数字时,它仍然会失去精度,所以不要使用数字):

// String
   BigInt("1234567890987654321234567890")
   // -> 1234567890987654321234567890n

// Number
   BigInt(1234567890987654321234567890)
   // -> 1234567890987654297979715584n
   /* As said above, using a number in the constructor may lose precision,
      as seen here just input a string or use the n */

// BigInt
   BigInt(1234567890987654321234567890n)
   // -> 1234567890987654321234567890n

// Boolean
   BigInt(false) // 0n
   BigInt(true)  // 1n

您不能在操作中混用类型。您不能对普通 JavaScript 数字进行任何正常操作,您必须先将它们转换为 BigInts。

let big_int_number = BigInt(135445264365246564)

// Incorrect
big_int_number + 1365

// Correct (either one)
big_int_number + 1365n
big_int_number + BigInt(1365)

最后回答真正的问题:要将 BigInt 转换为字符串,您只需要使用内置方法 3:

let big_int_number = BigInt("135445264365246564")
// or
let big_int_number = 135445264365246564n

// Using BigInt.prototype.toString()
let string_version = big_int_number.toString()


/* You probably don't need this method,
   but I just put it here just because */

// Using BigInt.prototype.toLocaleString()
let string_version = big_int_number.toLocaleString()

这适用于两种构造方式...

let n_constructed = 1234567890987654321234567890n
    n_constructed.toString()       // 1234567890987654321234567890n
    n_constructed.toLocaleString() // 1234567890987654321234567890n

let constructered = BigInt("1234567890987654321234567890")
    constructered.toString()       // 1234567890987654321234567890n
    constructered.toLocaleString() // 1234567890987654321234567890n

如果您对 BigInt 有任何疑问,请访问 MDN's reference page on BigInt