Skip to content Skip to sidebar Skip to footer

Converting Big Number String To Number

What way i can convert string with 16 digits and 2 fraction value to number ? Currently when I try to convert Number('1234567890123456.12') will became to 1234567890123456. fractio

Solution 1:

Unfortunately not. Javascript represents it's numbers using double precision floating point numbers. At 16 digits, it will only be able to store the integer component and not the part after the decimal point. You will need a bignum library to use this value.

EDIT: for reference the biggest integer you can use in JavaScript is 9,007,199,254,740,991

EDIT2: Thanks to Jeremy you can use a library like bignumberJS.

Solution 2:

Your number has too many algorisms, I've created an example that simulates in the first position of the array the maximum length possible in javascript.

var nums = [
    "12345678910111.12", 
    "1.5323",  
    "-42.7789"
];

nums.forEach(function(n) {
    console.log(parseFloat(n).toFixed(2));
});

https://jsfiddle.net/7zzz1qzt/

Solution 3:

I have faced issue to convert 18 digit string number to number. It is convert all digit to 0 after 16 digit. I have apply below code. it is working fine for me.

[{"id":${id},"name":"${name}"}]

Post a Comment for "Converting Big Number String To Number"