JavaScript uses IEEE 754 double-precision floats to represents numbers. That works perfectly fine for small numbers, however, it is an issue for big integers. This means they lose integer precision for values beyond +/- 2 pow 53
Problem
Presentation of small integer in decimal format works fine (e.g. 0x1FF
). However, we can see an issue when try to convert big integers like 0x1234567890abcdeffedcba908765421
to string decimal.
0x1FF // returns '511' - correct0x1234567890abcdeffedcba908765421 // returns '1.5123660750094533e+36' - incorrect - lose integer precision
Solution
Node.js biguint-format
module has been built in order to help display very large unsigned integers without any integer precision lose. biguint-format
takes an array of bytes (values from 0 to 255) or node Buffer and converts it to decimal format.
var biguint = ; // 0x1234567890abcdeffedcba908765421 split into bytesbiguint // output value is '1512366075009453296626403467035300897' - no integer precision lose
biguint-format
can also take array of bytes in Big Endian (BE
- default value) and Little Endian (LE
) formats. Check wikipedia for more details.
var biguint = ; var buffer1 = 0x63 0xA7 0x27;var buffer2 = 0x27 0xA7 0x63; biguint // returns '2598755'biguint // returns '2598755'biguint // returns '2598755'