TypeScript and Large Number

本文探讨了在TypeScript中处理超出安全整数范围的大整数时的注意事项及解决方案,包括使用字符串存储大整数、利用big-integer库进行纯JavaScript/TypeScript的处理以及在需要高性能时采用bignum库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TypeScript and Large Number

Don't use the Number type to store or calculate numbers that could exceed TypeScript's safe integer range of -9007199254740991 to 9007199254740991.
* If the number only needs to be transported or presented, consider using a String instead.
* Integers will be natively supported in TypeScript 3.1.
* The big-integer library can be used for a pure JS/TS workaround.https://www.npmjs.com/package/big-integer
* The bignum library can be used when performance is needed (uses OpenSSL) https://www.npmjs.com/package/bignum

var obj = JSON.parse('{ "id1": "-4653387195933842958", "id2": "-4653387195933842959" }');
console.log(obj.id1); //-4653387195933842958
console.log(obj.id2); //-4653387195933842959

var obj = JSON.parse('{ "id1": -4653387195933842958, "id2": -4653387195933842959 }');
console.log(obj.id1); //-4653387195933843000
console.log(obj.id2); //-4653387195933843000

-9, 007, 199, 254, 740, 991
-4, 653, 387, 195, 933, 842, 959

JSON.parse will always have this issue.

References:
https://www.webtoolkitonline.com/javascript-tester.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值