JavaScript

How to convert string to float in JavaScript

Knowing the procedure to convert string to float is critical when you want to perform some mathematical operation on the string value. For instance, the user inputs temperature as a string type, and it is required to utilize that string for generating the weekly weather report. To do so, it is necessary to convert the specified string into a float number before further processing, and JavaScript built-in methods can assist you in this regard.

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

var float = +(string)

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:

function convert(x) {
    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:

var a = "63.23";
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

parseFloat(string)

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:

var a = parseFloat("32.54");
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:

var a = parseFloat("    220   ");
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:

var a = parseFloat("[email protected]");
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):

var a = parseFloat("[email protected]");
console.log("After conversion: "+ a);

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:

var a = parseFloat("20 45 23456");
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.

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.