Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 957
Valip Avatar asked Nov 18 '16 20:11

Valip


People also ask

How do I convert a string to a number in JavaScript?

How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.

What function converts a string to a number?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

How do you convert a string to a number in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.


2 Answers

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

like image 66
Geeky Avatar answered Sep 22 '22 06:09

Geeky


It looks like you're looking for the Number() functionality here:

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

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

Hope this helps!

like image 21
Dave Cooper Avatar answered Sep 23 '22 06:09

Dave Cooper