![]() |
Converting strings to numbers in JavaScript is a fundamental operation, crucial for various tasks like handling user inputs or data processing. By converting strings to numeric values, JavaScript enables developers to perform mathematical operations and comparisons accurately, enhancing the versatility and functionality of their applications. Table of Content Using the ‘+’ operatorThe + 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.
Output Type of str before conversion: string Type of str after conversion: number Using the Number() constructorThe 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.
Output Type of str before conversion: string Type of str after conversion: number Using the parseInt() methodThe 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.
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() methodThe 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.
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() methodThe 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.
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() methodThe 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.
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() methodThe 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.
Output Before Conversion: str1: 233 , Type: string str2: 23.33 , Type: string After Conversion: str1: 233 , Type: number str2: 23 , Type: number |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |