A string is nothing but a sequence of characters put together. However, some problems require the conversion of string data type to some other data type. In this article, we will look into Strings in JavaScript and the techniques to convert a String to Float in JavaScript. Let us get started!
How to Convert String to Float in JavaScript?
Strings in JavaScript represent a sequence of multiple characters. ‘C’ represents a single character and “Cat” represents a string. In JavaScript, we can declare a string in two ways.
There are various techniques that help us convert a JavaScript String to Float. We will discuss 5 different methods with examples.
1) Using parseFloat() Functions
The easiest method to convert a string to a float number is by using the parseFloat() function. The parseFloat() function returns a float or NaN.
The syntax of parseFloat() is as follows:
let num = parseFloat(value);
where ‘value’ can be string, float, etc.
Let us see an example:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //using the parseFloat() function let convertedNumFloat=parseFloat(strFloat); //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13.65 number
In this example, we have first taken a strFloat variable to store a string equal to “13.65”. We checked the type of the variable using the “typeof()” function. We used the parseFloat() function to convert the string to a float number. We finally check the “convertedNumFloat” variable type to verify whether the Number() function has converted the string to a float number.
We have got the desired output as a float.
2) Using Number() Function
Another method is the simple “Number()” function. The syntax of the Number() function is as follows:
let num = Number(value);
where,
num: a variable to store the number we get after passing any value as a parameter
value: can be a string, float, etc.
Here’s an example of using the Number() Function to convert string to float:
//declaring a variable which is a string let str="15.7"; //printing the "str" variable and it's type console.log(str + " " + typeof(str)); //using the Number() function let num=Number(str); //printing the "num" variable and it's type console.log(num + " " + typeof(num));
Output:
15.7 string
15.7 number
In this example, we have first taken a str variable to store a string equal to “15.7”. We checked the type of the variable using the “typeof()” function. We used the Number() function to convert the string to a float number. We finally check the type of the “num” variable to verify whether the Number() function has converted the string to a float number. We have obtained the desired result.
3) Using the Unary Plus Operator
In JavaScript, the unary plus operator (+) is a simple and concise way to convert a string to a float number. Adding the “+” operator before a string in JavaScript automatically converts the string to a numeric value (type conversion).
This method works for the float values in the following way:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and it's type console.log(strFloat + " " + typeof(strFloat)); //converting string to number using unary operator let convertedNumFloat=+strFloat; //printing the "convertedNumFloat" variable and it's type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13.65 number
The string “13.65” has been converted to a float number 13.65.
4) Subtracting the Number 0 from the String
The next approach to convert a string to a float number is by subtracting 0 from the string. JavaScript automatically performs a type conversion while subtracting 0 from the string, converting a string to a number.
Let us see an example:
//declaring a variable which is a string let strFloat="13.65"; //printing the "strFloat" variable and its type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by subtracting 0 from it let convertedNumFloat=strFloat-0; //printing the "convertedNumFloat" variable and its type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.65 string
13.65 number
We have obtained the desired output.
5) Multiplying and Dividing the String by 1
The best approach to convert a string into a float number is by multiplying or dividing the string value by 1. JavaScript automatically performs a type conversion, converting the string into a float number.
Here’s an example of converting a float string number to an actual float number by multiplying it by 1.
//declaring a variable which is a string let strFloat="13.45"; //printing the "strFloat" variable and its type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by multiplying with 1 let convertedNumFloat=strFloat*1; //printing the "convertedNum" variable and its type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.45 string
13.45 number
Here’s an example of converting a float string to an actual float number by dividing it by 1:
//declaring a variable which is a string let strFloat="13.45"; //printing the "strFloat" variable and its type console.log(strFloat + " " + typeof(strFloat)); //converting string to number by dividing with 1 let convertedNumFloat=strFloat/1; //printing the "convertedNum" variable and its type console.log(convertedNumFloat + " " + typeof(convertedNumFloat));
Output:
13.45 string
13.45 number
We have obtained the desired result.
These were some of the methods to convert a string to a float. You can also learn how to convert string to array in an easier way.
Conclusion
In this article, we discussed various JavaScript techniques to convert String to Float data type It is essential to know at least three of the methods when working in JavaScript as they come in very handy at times.