I have some Ids I need converted from strong to numbers however whenever I use parseInt() or Number(), I get a double type decimal
1.01038295007818e+016.0
I need to just convert the string to just a number.
I have some Ids I need converted from strong to numbers however whenever I use parseInt() or Number(), I get a double type decimal
1.01038295007818e+016.0
I need to just convert the string to just a number.
The maximum integer that can be represented in javascript, without losing any precision, is 9007199254740991. See the javascript docs for more info. If you want to use integers bigger than that, you will need to use a third party library like Big Integer.
The parseInt() function takes the first number of the string and removes decimal places, it returns an integer:
var a = parseInt("1.000350001");
var b = parseInt("456789.1");
var c = parseInt("20");
console.log(a);
console.log(b);
console.log(c);
The Math.floor() method rounds a number downward to its nearest integer:
var d = Math.floor("1234.00101");
console.log(d);
The Number() function converts the string to a number that represents the strings value:
var e = Number("1000.12");
console.log(e);
For your question, I believe either parseInt or Math.floor would work just fine, unless your number is greater than JavaScripts largest available number
There are 5 different ways to convert string to number as below:
parseInt(num); // default way (no radix)
parseInt(num, 10); // parseInt with radix (decimal)
parseFloat(num) // floating point
Number(num); // Number constructor
~~num //bitwise not
num / 1 // diving by one
num * 1 // multiplying by one
num - 0 // minus 0
+num // unary operator "+"
You will find thye explanation Here
for
var s = parseInt("101038295007818e0160",10);
alert(s);
O/P: 101038295007818e0160 .
and for:
var s = parseInt(101038295007818e0160,10);
alert(s);
O/P:1
for
var s = parseInt("101038295007818e0160");
alert(s);
O/P: 101038295007818e0160 .
and for:
var s = parseInt(101038295007818e0160);
alert(s);
O/P:1
Hope it will help you.
What you're getting is scientific notation, because the number you've tried to parse it too big.
The largest integer accurately represented in Javascript is
9007199254740991
However your number 1.01038295007818e+016.0 is
10103829500781800