JavaScript

How to Convert Strings to Numbers in JavaScript

Data Management is one of the essential concepts in programming. As a result, JavaScript provides various functions for parsing data types, which permits you to easily convert the data format. For instance, if you want to perform any mathematical operations on a string comprising a number, you have to convert that string into a number first.

For this purpose, there exist many JavaScript methods such as parseInt(), parseFloat(), Number(), Math.floor(), and Math.ceil(). This write-up will demonstrate the method of converting strings to numbers in JavaScript using all of the mentioned functions. So, let’s start!

How to Convert Strings to Numbers in JavaScript using parseInt() method

In JavaScript, the “parseInt()” function is utilized for converting a string to a number. It takes “string” and “radix” as parameters and then converts the added string into an integer. In the parseInt() function, the radix is added to specify the numerical system you want to use.

For instance, the radix “8” indicates that the added number in the string will be converted from octal to decimal base. This method will output “NaN” Not a Number if the specified string does not include a numeric value.

Syntax of parseInt()

Now, check out the syntax of parseInt() function for converting strings to numbers:

parseInt(String, Radix)

In the above-given syntax, add the “String” which you want to convert as a first argument. Next, add a base as radix which is optional. This function will return a numeric value resulting from the string to number conversion.

Example: Converting Strings to Numbers in JavaScript using parseInt() without radix

In the first example, we will try to convert the “22a” string to a number using the parseInt() function. For this purpose, we will write out the following code in our console window:

myString = '22a'

console.log(parseInt(myString))

The parseInt() function will convert the value of “myString” to its corresponding number and show you the result with the help of the console.log() function:

Example: Converting Strings to Numbers in JavaScript using parseInt() with radix

As mentioned earlier, you can also add the base as “radix” for the conversion. For instance, we have defined “22” as a string and then set its radix as “8” in the parseInt() function. The result will be calculated as (2+2*8), which is “18”:

myString = '22'

console.log(parseInt(myString, 8))

How to Convert Strings to Numbers in JavaScript using parseFloat() method

If you want to convert a string into a floating-point number, you have to use the parseFloat() JavaScript function. It only returns the number found at the starting of the specified string that has been parsed until the parseFloat() function comes across a character that is not a number.

Syntax of parseFloat()

Here is the syntax of parseFloat() function for converting strings to numbers in JavaScript:

parseFloat(String)

The parseFloat() function accepts a single parameter as a “String” you want to add for conversion purposes.

Example: Converting Strings to Numbers in JavaScript using parseFloat() method

The following example will convert our “testString” having the value “224.56b” to a number using the parseFloat() function and the output of the parseFloat() function can be seen in the console window:

let testString = parseFloat("224.56b") ;

console.log(testString);

How to Convert Strings to Numbers in JavaScript using Number() method

The Number() function is used for converting JavaScript objects or variables to numbers. It can also be utilized for converting the added string to the number. This method will output “NaN” if the added string is not capable of being converted to a number.

Syntax of Number()

Have a look at the syntax of the Number() function in JavaScript:

This Number() function will convert any data type JavaScript object to a number and return its value.

Example: Converting Strings to Number in JavaScript using Number() method

In the below-given example, we will convert the “testString” JavaScript variable to a number using the “Number()” function:

let testString = Number("101.11");

console.log(testString);

The output declares that we have successfully converted the added string to a number:

How to Convert Strings to Numbers in JavaScript using Math.floor() method

The Math.floor() function in JavaScript returns the largest integer value less than or equal to the number that is passed. This function also takes strings and converts them to integers.

Syntax of Math.floor()

Math.floor(Value)

The above-given Math.floor() function will take the specified string as “Value” and then convert it to a number.

Example: Converting Strings to Number in JavaScript using Math.floor() method

Now, we will utilize the Math.floor() function to convert the “101.11” string to a number:

let testString = Math.floor("101.11");

console.log(testString);

How to Convert Strings to Numbers in JavaScript using Math.ceil() method

The Math.ceil() function in JavaScript is utilized to display the smallest integer greater than or equal to the specified number. You can also utilize this function for converting strings to numbers.

Syntax of Math.ceil()

Math.ceil(Value)

The Math.ceil() function can take the added string as “Value” and then convert it to a number.

Example: Converting Strings to Numbers in JavaScript using Math.ceil() method

For instance, to convert the “578.99” string to number in JavaScript, we will use the Math.ceil() function in the following way:

let testString = Math.ceil("578.99");

console.log(testString);

The Math.ceil() function will round off the “578.99” string to the “579” number:

Conclusion

To convert strings to numbers in JavaScript several methods are used such as parseInt(), parseFloat(), Number(), Math.floor(), and Math.ceil() and this write-up contains the detailed description and examples of all these methods. All of the given methods are easy and simple to use. You can utilize any of them for converting your selected string into a number.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.