This post will discuss different methods for a string to float conversion in JavaScript. So, let’s start!
How to convert string to float in JavaScript
In JavaScript, you can convert string to float using the following approaches:
- Convert string to float using JavaScript “Type Conversion” feature
- Convert string to float using JavaScript “parseFloat()” method
We will now explain each of the above-mentioned methods in detail.
How to convert string to float in JavaScript using Type Conversion
“Type Conversion” is a JavaScript feature that forces the compiler to change the given expression or value type. In JavaScript, there exist several operators that are used for converting data types. For instance, the Unary operator “+” converts string value to float.
Syntax
Here, the Unary operator “+” will convert the type of “string” value to a “float” number.
Example
First of all, we will define a “convert()” function that accepts a “string” argument and converts it to “float” using the Unary operator “+”. This function will return the floating-point number:
var floatValue = +(x);
return floatValue;
}
In the next step, we will create a string variable “a” having the value “63.23” and pass it to the “convert()” function:
console.log("Before converting "+ a + " , type: " + typeof a);
newFloat = convert(a);
console.log("After converting "+ newFloat + " , type: " + typeof newFloat);
The given output signifies that the type of “63.23” is successfully converted from “string” to “float”:
Now, let’s check out the other method for converting string to float in JavaScript.
How to convert string to float in JavaScript using parseFloat() method
JavaScript offers a built-in method named “parseFloat()” that can be utilized for converting string to a floating-point number. This method accepts the specified string “value” as an argument and then outputs the converted float number.
Syntax
Here, the JavaScript “parseFloat()” method takes “string” as an argument and returns a floating-point number.
Example 1
We will now convert the value of the string “32.54” into a floating-point number by using the “parseFloat()” method:
console.log("After converting "+ a + ", type: " + typeof a);
As you can see, the “type” of the passed “string” argument is now changed to “number”:
Example 2
If the specified string contains trailing and leading spaces, then the “parseFloat()” method will ignore them and return the floating-point number:
console.log("After conversion: "+ a);
console.log("Type: " + typeof a);
Execution of the above-given code will convert the string ” 220 ” into float “220”:
Example 3
In case, if the string comprises different types of values, then the JavaScript “parseFloat()” method will return the floating point number until it reaches any non-numeric character:
console.log("After conversion: "+ a);
console.log("Type: " + typeof a);
Here, the “parseFloat()” method will convert the string “[email protected]” to “2022” floating point number:
Example 4
For non-numeric characters, the “parseFloat()” number will return “NaN” (Not a Number):
Execution of the given “parseFloat()” method will return “NaN” for the passed string “[email protected]” as it starts with a non-numeric character:
Example 5
In a scenario where the passed “string” argument comprises spaces in between numbers, then the JavaScript “parseFloat()” method will only return the first encountered number.
For instance, in the following example, we will pass the “20 45 23456” string as an argument to the “parseFloat()” method, and it sets the first number “20” as its return case:
console.log("After conversion: "+ a);
console.log("Type: " + typeof a);
Output
That was all the essential information related to converting a string to float in JavaScript. You can further research according to your preferences.
Conclusion
To convert string to float, you can use the “Type Conversion” JavaScript feature or the “parseFloat()” method. Type conversion utilizes the Unary operator “+” for converting string value to float, whereas the “parseFloat()” method accepts a string “value” as an argument and returns the converted float number. This write-up discussed different methods for converting strings to floating-point numbers in JavaScript.