简体   繁体   English

将大字符串数字转换为数字

[英]Convert Large String Number to Number

I have this string我有这个字符串

let a = '1589480087711852280'

How do I convert this to a number?如何将其转换为数字?

If I do如果我做

let a = '1589480087711852280'
a = Number(a)
console.log(a)

It prints 1589480087711852300 and just rounds it.它打印1589480087711852300并对其进行四舍五入。

How do I get it to print 1589480087711852280 ?如何让它打印1589480087711852280

It tried BigInt and that just puts an n at the end of the number.它尝试了 BigInt 并且只是在数字的末尾放了一个 n 。

I'm using this to query within mongodb so its not pulling the right search.我正在使用它在 mongodb 中进行查询,因此它没有进行正确的搜索。

Use BigInt :使用BigInt

let a = '1589480087711852280'
a = BigInt(a)
console.log(a + 1n) // 1589480087711852281n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM