Javascript解析int64 [英] Javascript parsing int64

查看:1125
本文介绍了Javascript解析int64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有javascript四舍五入的情况下将长整数(作为字符串)转换为Javascript中的数字格式?

How can I convert a long integer (as a string) to a numerical format in Javascript without javascript rounding it?

var ThisInt = '9223372036854775808'
alert(ThisInt+'\r' +parseFloat(ThisInt).toString()+'\r' +parseInt(ThisInt).toString());

我需要对其进行添加,然后再将其作为字符串&如果可能的话,我宁愿不要把它切成两半。

I need to perform an addition on it before casting it back as a string & would prefer not to have to slice it two if at all possible.

推荐答案

全部 Javascript中的数字是64位双精度 IEE754 浮点。

All Numbers in Javascript are 64 bit "double" precision IEE754 floating point.

因此可以准确表示的最大正整数是2 ^ 53 - 1.其余位是为指数保留的。

The largest positive whole number that can therefore be accurately represented is 2^53 - 1. The remaining bits are reserved for the exponent.

你的数字正好比那个大1024倍,因此丢失3个十进制数字的精度。它无法更准确地表示。

Your number is exactly 1024 times larger than that, so loses 3 decimal digits of precision. It simply cannot be represented any more accurately.

在ES6中,可以使用 Number.isSafeInteger(#)来测试一个数字,看它是否在安全范围内:

In ES6 one can use Number.isSafeInteger( # ) to test a number to see if its within the safe range:

var ThisInt = '9223372036854775808'; 
console.log( Number.isSafeInteger( parseInt( ThisInt ) ) );

还有一个 BigInteger 库,应该可以提供帮助,但是,避免你必须做所有的字符串和比特自己。

There is also a BigInteger library available which should be able to help, though, and avoid you having to do all the string and bit twiddling yourself.

这篇关于Javascript解析int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆