2

Convert a String to Number in JavaScript

 7 months ago
source link: https://www.geeksforgeeks.org/convert-a-string-to-number-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In JavaScript, you can store a numeric value in the form of a string which may lead to generating undesired results when you try to operate mathematical operations on them. This problem arises when you take a numeric input from the user using the input elements in HTML. They return the entered value in the form of a string. You can use the below methods to change the returned string into a number.

Using the ‘+’ operator

The + operator can be operated with the string by using it before the name of the string variable to convert it into a number.

Syntax:

+stringVariable;

Example: The below code implements the + operator to convert a string into a number.

  • Javascript
const str = "233";
console.log("Type of str before conversion: ", typeof str);
console.log("Type of str after conversion: ", typeof +str);
Output
Type of str before conversion:  string
Type of str after conversion:  number

Using the Number() constructor

The Number() constructor can be used to convert a string into a number by passing the string value as an argument to it.

Syntax:

Number(stringVariable);

Example: The below code converts a string to number with the help of Number() constructor.

  • Javascript
const str = "233";
console.log("Type of str before conversion: ", typeof str);
console.log("Type of str after conversion: ", typeof Number(str));
Output
Type of str before conversion:  string
Type of str after conversion:  number

Using the parseInt() method

The parseInt() method can be used to convert a numeric string into a number by passing it as an arguement to it. It will always return an integer either you pass a floating point string or integer string.

Syntax:

parseInt(stringVariable);

Example: The below code uses the parseInt() method to convert a string into a number.

  • Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", parseInt(str1), ", Type: ", typeof parseInt(str1));
console.log("str2: ", parseInt(str2), ", Type: ", typeof parseInt(str2));
Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  23 , Type:  number

Using the parseFloat() method

The parseFloat() method can also be used to convert a string into a number in the same way we use the parseInt() method by passing the string value to it. It will return the value as it is as passed to it.

Syntax:

parseFloat(stringVariable);

Example: The below code is practical implementation of the parseFloat() method to convert a string into a number.

  • Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", parseFloat(str1), ", Type: ", typeof parseFloat(str1));
console.log("str2: ", parseFloat(str2), ", Type: ", typeof parseFloat(str2));
Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  23.33 , Type:  number

Using the Math.floor() method

The Math.floor() method will return the low value if the floating point string is passed to it and convert its type from string to number.

Syntax:

Math.floor(stringVariable);

Example: The below code illustrates the use of Math.floo() method to convert a string into a number.

  • Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.floor(str1), ", Type: ", typeof Math.floor(str1));
console.log("str2: ", Math.floor(str2), ", Type: ", typeof Math.floor(str2));
Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  23 , Type:  number

Using the Math.ceil() method

The Math.ceil() method can be used in the same way as the Math.floor() method was used. It will convert the string type from string to number and returns the upper value if floating point string passed to it.

Syntax:

Math.ceil(stringVariable);

Example: The below code ecplains the use of the Math.ceil method to convert string to a number.

  • Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.ceil(str1), ", Type: ", typeof Math.ceil(str1));
console.log("str2: ", Math.ceil(str2), ", Type: ", typeof Math.ceil(str2));
Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  24 , Type:  number

Using the Math.round() method

The Math.rond() method will round up the passed floating string and returns its value after rounding it up. It also converts the type of string to number.

Syntax:

Math.round(stringVariable);

Example: The below code example uses the Math.round() method to convert a string into a number.

  • Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.round(str1), ", Type: ", typeof Math.round(str1));
console.log("str2: ", Math.round(str2), ", Type: ", typeof Math.round(str2));
Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  23 , Type:  number

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!

Three 90 Challenge ending on 5th Feb! Last chance to get 90% refund by completing 90% course in 90 days. Explore offer now.

Last Updated : 02 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK