How can I handle numbers bigger than 17-digits in Firefox/IE7?

流过昼夜 提交于 2019-12-28 06:32:28

问题


For a web application I want to be able to handle numbers up to 64 bits in size. During testing, I found that javascript (or the browser as a whole) seems to handle as much as 17 digits. A 64-bit number has a maximum of 20 digits, but after the javascript has handled the number, the least significant 3 digits are rounded and set to 0.... Any ideas where this comes from? More importantly, any idea how to work around it?


回答1:


In Javascript, all numbers are IEEE double precision floating point numbers, which means that you only have about 16 digits of precision; the remainder of the 64 bits are reserved for the exponent. As Fabien notes, you will need to work some tricks to get more precision if you need all 64 bits.




回答2:


I think you need to treat them as strings if you have reached Javascript limit (see here)




回答3:


As others note, JS implements doubles, so you'll have to look elsewhere to handle bigger numbers. BigInt is a library for arbitary precision math for integers.




回答4:


You could try to split them into two or more numbers (in a class maybe), but you'll might need some arithmetic helper functions to work with them.

Cheers



来源:https://stackoverflow.com/questions/1086575/how-can-i-handle-numbers-bigger-than-17-digits-in-firefox-ie7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!