How to convert a string to a number

I cannot figure out how to convert this line 82144251 to a number.

The code:

 var num = "82144251"; 

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

Question update: I am using the Google Apps Script Editor and this should be the problem ...

 num = parseInt(num).toFixed() // if I just do parseInt(num) it returns 8.2144251E7 
+10
source share
6 answers

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

check out these fragments

 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 help

+15
source

No questions are stupid.

quick response:

to convert a string to a number, you can use a unary plus.

 var num = "82144251"; num = +num; 

The execution of num = +num almost the same as the execution of num = num * 1; , if necessary, it converts the value of to into a number, but after that it does not change the value.

+3
source

It looks like you are looking for Number() functionality here:

 var num = "82144251"; // "82144251" var numAsNumber = Number(num); // prints 82144251 typeof num // string typeof numAsNumber // number 

You can learn more about Number() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

Hope this helps!

+2
source

 var num = "82144251"; num = parseInt(num).toFixed() console.log(num, typeof num); // string num = parseFloat(num); console.log(num, typeof num); // number 
0
source

var intNum = parseInt ("82144251", 10); // intNum - number

0
source

I am using num-0 since it is easier for inline use.

0
source

Source: https://habr.com/ru/post/1012467/


All Articles