Search code examples
javascriptecmascript-6numbershexparseint

Parseint – truncating with e+


I am using parseInt() internally to convert a value that I now want to convert to another number system with toString().

parseInt(value, 10).toString(16) 

But since the integer is being truncated (ie 1.7956279830335669e+47) because of length, I am unable to achieve the desired hexadecimal representation of my number.

Is there some other way to cast a string to an int?
How can I go about resolving this?


Solution

  • Use BigInt:

    BigInt(value).toString();
    

    You need it because the largest Number JavaScript can support is 9007199254740991:

    console.log(Number.MAX_SAFE_INTEGER);

    You can use BigInt because it's a built-in global object, however it's not a fully supported ECMAScript feature - currently it's in Stage 3 of development.