1

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.

1
  • 3
    Can you Just mention the string you want to convert to number?
    – techhunter
    Nov 13, 2016 at 6:16

4 Answers 4

5

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.

0
1

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

1
  • 1
    Answer was number is too big! Thanks for the extra informative answer though!
    – deek
    Nov 13, 2016 at 21:29
1

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.