JavaScript parseFloat () function; Through this tutorial, i am going to show you how to convert string to float number using parseFloat() with the help of examples.
Convert a String to float Number in javascript
- JavaScript parseFloat() Function
- JavaScript parseFloat() Function Syntax
- Examples Convert a String to float Number in javascript
JavaScript parseFloat() Function
The parseFloat() function is used to accept a string and convert it into a floating-point number. If the input string does not contain a numeral value or If the first character of the string is not a number then it returns NaN i.e, not a number. This function returns a floating-point number parsed up to that point where it encounters a character that is not a number.
Syntax of JavaScript parseFloat() Function
The syntax of javascript parseFloat () is very simple. The JavaScript parseFloat () should look like this :
parseFloat(string)
- Params : It accepts a parameter “string value” that contains a string that is changed to floating-point number.
To understand what parseFloat is in JavaScript, it is necessary to see an example. Take a look at this parseFloat function example – where we parse different numbers and strings :
Example 1 – Convert a String to float Number in javascript
<script> // return float value a = parseFloat(" 10 ") document.write('parseFloat(" 10 ") = ' +a +"<br>"); b = parseFloat("123abc") document.write('parseFloat("123abc") = '+b +"<br>"); // returns NaN value c = parseFloat("abc456") document.write('parseFloat("abc456") = ' +c +"<br>"); d = parseFloat("3.14") document.write('parseFloat("3.14") = '+d +"<br>"); // returns only first Number e = parseFloat("18 2 2019") document.write('parseFloat("18 2 2019") = ' +e +"<br>"); </script>
Output
parseFloat(" 10 ") = 10
parseFloat("123abc") = 123
parseFloat("abc456") = NaN
parseFloat("3.14") = 3.14
parseFloat("18 2 2019") = 18