js protobuf int64 解决方案

// 2^64
var int64_comm1 = 0xFFFFFFFFFFFFFFFF;
var int64_comm2 = 18446744073709551615;
var jsint64 = 18446744073709552000;
console.log(int64_comm1);
console.log(int64_comm2);
console.log(jsint64);
var ThisInt = '18446744073709551615';
console.log(parseInt(ThisInt));
console.log(Number.isSafeInteger(parseInt(ThisInt)));
 

https://github.com/dcodeIO/long.js

判断是否可以安全转化(从string 到js 53位的int)


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

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

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

In ES6one 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 ) ) );


版权声明:本文为LANGZI7758521原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。