# JavaScript Cast To Float

JavaScript Cast To Float | When we need to execute a mathematical operation on a string value, we’ll need to know how to convert string to float. For example, the user enters temperatures as a string type, and the monthly climate report must be generated using that string. To do so, we need to convert the provided string to a float number before continuing, and JavaScript’s built-in methods can help you with that.

It’s vital to know what we are doing with integers and decimal places in JavaScript, but the most crucial part is to know how to do it.

This JavaScript tutorial shows you how to use the parseFloat () function in JavaScript to convert string to float. You will learn what parseFloat is and what it does. After reading this article, you’ll understand the JavaScript parsing() syntax and when it should be used.

Syntax:– parseFloat(string)

The “string” input is passed to the JavaScript “parseFloat()” technique, which generates a floating-point value.

Let us have a look at some examples that will help you understand the topic, just a bit better. You can check the type of the string argument by using “typeof” function.

## JavaScript Cast To Float Example

Example 1:
This example will show you how to convert a string “88.19” into a floating-point value using parsing in JavaScript.

``````var num = parseFloat("88.19");
console.log(num)``````

Output:-

88.19

Example 2:
The parseFloat() method returns the floating-point value of the string, ignoring leading and trailing spaces.

The following program will convert the string ”   324   ” into the float “324”:-

``````var num = parseFloat("    324   ");
console.log(num)``````

Output:-

324

Example 3:
If the string contains many sorts of values, the JavaScript “parseFloat()” method will return a floating-point number until it encounters a non-numeric element. The “parseFloat()” technique will translate the string “2025#Knowprogram” to a floating-point number of “2025” in this case.

``````var str = parseFloat("[email protected]");
console.log(str)``````

Output:-

2025

Example 4:
The “parseFloat()” value will yield “NaN” for non-numeric elements because the provided text “[email protected]” begins with a non-numeric letter, the “parseFloat()” method will return “NaN”. For example,

``````var str = parseFloat("[email protected]");
console.log(str)``````

Output:-

NaN

Example 5:
If the given “string” input has spaces between integers, the JavaScript “parseFloat()” function would only output the first number detected.

For example, in the given example, we’ll provide the string “34 954 09403” as a parameter to the “parseFloat()” method, which will return the initial number “34”.

``````var str = parseFloat("34 954 09403");
console.log(str)``````

Output:-

34

Other conditions:

1. When parseFloat comes across a character that isn’t a plus (+), minus (-), numerals (0–9), decimal (.), or exponent (e or E), it yields the value up to that letter, disregarding the ineligible letters and characters after it.
2. Parsing is also halted by the addition of a second decimal point.
3. In addition, parseFloat can parse and output Infinity.
4. BigInt syntax is converted to Numbers using parseFloat, but precision is lost. Since the trailing n character is discarded, this occurs.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!