如何在 Firefox/IE7 中处理大于 17 位的数字?

2022-01-17 00:00:00 numbers javascript

对于一个 Web 应用程序,我希望能够处理最大为 64 位的数字.在测试过程中,我发现 javascript(或整个浏览器)似乎可以处理多达 17 位数字.一个 64 位的数字最多有 20 位数字,但是在 javascript 处理完数字后,将最低有效 3 位四舍五入并设置为 0....任何想法这是从哪里来的?更重要的是,知道如何解决它吗?

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?

推荐答案

在 Javascript 中,所有数字都是 IEEE double精度 浮点数,这意味着您只有大约 16 位的精度;剩余的 64 位为指数保留.正如 Fabien 所说,如果您需要全部 64 位,则需要使用一些技巧来获得更高的精度.

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.

相关文章