Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript parseFloat() Function

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

The JavaScript 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 accepts a single parameter as mentioned above and described below:

  • value: This parameter obtains a string that 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 is an example of the parseFloat() function.

Example 1: 

javascript




<script>
    var v2 = parseFloat("3.14");
      
    console.log('Using parseFloat("3.14") = ' 
    + v2);
</script>

Output:

Using parseFloat("3.14") = 3.14

Example 2: 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)

Example 3:

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




<script>
    // It ignores leading and trailing spaces.
    a = parseFloat(" 100 ")
    console.log('parseFloat(" 100 ") = ' +a);
      
    // It returns floating point Number until
    // it encounters Not a Number character
    b = parseFloat("2018@geeksforgeeks")
    console.log('parseFloat("2018@geeksforgeeks") = '
    +b);
      
    // It returns NaN on Non numeral character
    c = parseFloat("geeksforgeeks@2018")
    console.log('parseFloat("geeksforgeeks@2018") = '
    +c);
      
    d = parseFloat("3.14")
    console.log('parseFloat("3.14") = '
    +d);
      
    // It returns only first Number it encounters
    e = parseFloat("22 7 2018")
    console.log('parseFloat("22 7 2018") = '
    +e);
</script>

Output:

parseFloat(" 100 ") = 100
parseFloat("2018@geeksforgeeks") = 2018
parseFloat("geeksforgeeks@2018") = NaN
parseFloat("3.14") = 3.14
parseFloat("22 7 2018") = 22

Program 2: Using the isNaN() function to test whether the converted values are a valid numbers or not. 

javascript




<script>
    var x = parseFloat("3.14");
    if (isNaN(x))
        console.log("x is not a number");
    else
        console.log("x is a number");
      
    var y = parseFloat("geeksforgeeks");
    if (isNaN(y))
        console.log("y is not a number");
    else
        console.log("y is a number");
      
    // Difference between parseInt() and parseFloat()
    var v1 = parseInt("3.14");
    var v2 = parseFloat("3.14");
      
    console.log('Using parseInt("3.14") = ' 
    + v1);
    console.log('Using parseFloat("3.14") = ' 
    + v2);
</script>

Output:

x is a number
y is not a number
Using parseInt("3.14") = 3
Using parseFloat("3.14") = 3.14

We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!