javascriptgoogle-apps-scriptstring-conversion

How to convert a string to number


I can't figure it out how to convert this string 82144251 to a number.

Code:

var num = "82144251";

If I try the code below the .toFixed() function converts my number back to a string...

Question update: I'm using the Google Apps Script editor and that must be the issue...

num = parseInt(num).toFixed() // if I just do parseInt(num) it returns 8.2144251E7

Solution

  • You can convert a string to number using unary operator '+' or parseInt(number,10) or Number()

    check these snippets

    var num1a = "1";
    console.log(+num1a);
    
    var num1b = "2";
    num1b=+num1b;
    console.log(num1b);
    
    
    var num3 = "3"
    console.log(parseInt(num3,10));
    
    
    var num4 = "4";
    console.log(Number(num4));

    Hope it helps