JavaScript parseFloat() Function - GeeksforGeeks

09 Dec, 2021
Below is the example of the parseFloat() function.
Example:javascriptjavascript<script>    var v2 = parseFloat("3.14");      document.write('Using parseFloat("3.14") = '     + v2 + "<br>");</script>
javascript
<script>    var v2 = parseFloat("3.14");      document.write('Using parseFloat("3.14") = '     + v2 + "<br>");</script>
Output:Using parseFloat("3.14") = 3.14
Using parseFloat("3.14") = 3.14
The parseFloat() function is used to accept the string and convert it into a floating-point number. If the 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. It actually returns a floating-point number parsed up to that point where it encounters a character that is not a Number.
Syntax:
parseFloat(Value)
Parameters: This function accept a single parameter as mentioned above and described below:
value This parameter obtains a string which is converted to a floating-point number.
Return value: It returns a floating-point Number and if the first character of a string cannot be converted to a number then the function returns NaN i.e, not a number.
Below examples illustrate the parseFloat() function in JavaScript:
Example 1: The parseFloat() function ignores leading and trailing spaces and returns the floating point Number of the string.Input : var n = parseFloat("  2018  ");
Output: n=2018 (floating point Number)
Input : var n = parseFloat("  2018  ");
Output: n=2018 (floating point Number)
Example 2:Input: var a = parseFloat("1000.04");
Output:now a = 1000.04(floating point Number)More example codes for the above function are as follows:Program 1:javascriptjavascript<!DOCTYPE html><html>  <body><script>    // It ignores leading and trailing spaces.    a = parseFloat(" 100 ")    document.write('parseFloat(" 100 ") = ' +a +"<br>");      // It returns floating point Number until    // it encounters Not a Number character    b = parseFloat("2018@geeksforgeeks")    document.write('parseFloat("2018@geeksforgeeks") = '    +b +"<br>");      // It returns NaN on Non numeral character    c = parseFloat("geeksforgeeks@2018")    document.write('parseFloat("geeksforgeeks@2018") = '    +c +"<br>");      d = parseFloat("3.14")    document.write('parseFloat("3.14") = '    +d +"<br>");      // It returns only first Number it encounters    e = parseFloat("22 7 2018")    document.write('parseFloat("22 7 2018") = '    +e +"<br>");          </script>  </body></html>Output:parseFloat(" 100 ") = 100
parseFloat("2018@geeksforgeeks") = 2018
parseFloat("geeksforgeeks@2018") = NaN
parseFloat("3.14") = 3.14
parseFloat("22 7 2018") = 22Program 2: Using isNaN() function to test that converted values are valid number or not.javascriptjavascript<!DOCTYPE html><html>  <body><script>    var x = parseFloat("3.14");    if (isNaN(x))        document.write("x is not a number" + "<br>");    else        document.write("x is a number" + "<br>");      var y = parseFloat("geeksforgeeks");    if (isNaN(y))        document.write("y is not a number" + "<br>");    else        document.write("y is a number" + "<br>");      // Difference between parseInt() and parseFloat()    var v1 = parseInt("3.14");    var v2 = parseFloat("3.14");      document.write('Using parseInt("3.14") = '     + v1 + "<br>");    document.write('Using parseFloat("3.14") = '     + v2 + "<br>");</script>  </body></html>           Output:x is a number
y is not a number
Using parseInt("3.14") = 3
Using parseFloat("3.14") = 3.14Supported Browsers:Google Chrome 1 and aboveEdge 12 and aboveFirefox 1 and aboveInternet Explorer 3 and aboveSafari 1 and aboveOpera 3 and aboveMy Personal Notes
arrow_drop_upSave
Input: var a = parseFloat("1000.04");
Output:now a = 1000.04(floating point Number)
More example codes for the above function are as follows:
Program 1:
javascript
<!DOCTYPE html><html>  <body><script>    // It ignores leading and trailing spaces.    a = parseFloat(" 100 ")    document.write('parseFloat(" 100 ") = ' +a +"<br>");      // It returns floating point Number until    // it encounters Not a Number character    b = parseFloat("2018@geeksforgeeks")    document.write('parseFloat("2018@geeksforgeeks") = '    +b +"<br>");      // It returns NaN on Non numeral character    c = parseFloat("geeksforgeeks@2018")    document.write('parseFloat("geeksforgeeks@2018") = '    +c +"<br>");      d = parseFloat("3.14")    document.write('parseFloat("3.14") = '    +d +"<br>");      // It returns only first Number it encounters    e = parseFloat("22 7 2018")    document.write('parseFloat("22 7 2018") = '    +e +"<br>");          </script>  </body></html>
parseFloat(" 100 ") = 100
parseFloat("2018@geeksforgeeks") = 2018
parseFloat("geeksforgeeks@2018") = NaN
parseFloat("3.14") = 3.14
parseFloat("22 7 2018") = 22
Program 2: Using isNaN() function to test that converted values are valid number or not.
javascript
<!DOCTYPE html><html>  <body><script>    var x = parseFloat("3.14");    if (isNaN(x))        document.write("x is not a number" + "<br>");    else        document.write("x is a number" + "<br>");      var y = parseFloat("geeksforgeeks");    if (isNaN(y))        document.write("y is not a number" + "<br>");    else        document.write("y is a number" + "<br>");      // Difference between parseInt() and parseFloat()    var v1 = parseInt("3.14");    var v2 = parseFloat("3.14");      document.write('Using parseInt("3.14") = '     + v1 + "<br>");    document.write('Using parseFloat("3.14") = '     + v2 + "<br>");</script>  </body></html>           
x is a number
y is not a number
Using parseInt("3.14") = 3
Using parseFloat("3.14") = 3.14
Supported Browsers:
Google Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 3 and above
Safari 1 and above
Opera 3 and above
saurabh1990aror
ysachin2314
javascript-functions
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
How to read a local text file using JavaScript?
How to Use the JavaScript Fetch API to Get Data?