Pavel Valeriu Pavel Valeriu - 18 days ago 9
Javascript Question

How to convert a string to number using Google Apps Script

I know that I'm asking a dumb question, but 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


Example:
https://docs.google.com/spreadsheets/d/1l1QfAJoiJyVTqQdML-lrt4-pAfmgtTxY82xtKjR7mpw/edit?usp=sharing

Just select Tools and then Script Editor...

To see the logs press Ctrl + Enter

Answer

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

check these snippets

var num = "82144251";
num=+num
console.log(num);
var num2="82144251"
console.log(parseInt(num,10));

var num3="34";
console.log(Number(num3));

Hope it helps