Open In App

JavaScript Number parseFloat() Method

Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript parseFloat() Method 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 method accepts a single parameter.

  • 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() method:

Example 1: 

javascript




let v2 = parseFloat("3.14");
  
console.log('Using parseFloat("3.14") = ' + v2);


Output:

Using parseFloat("3.14") = 3.14 

Example 2: 

javascript




// 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);


Output: The parseFloat() function ignores leading and trailing spaces and returns the floating point Number of the string.

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

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

javascript




let x = parseFloat("3.14");
if (isNaN(x))
    console.log("x is not a number");
else
    console.log("x is a number");
  
let 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()
let v1 = parseInt("3.14");
let v2 = parseFloat("3.14");
  
console.log('Using parseInt("3.14") = '
    + v1);
console.log('Using parseFloat("3.14") = '
    + v2);


Output:

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

We have a complete list of JavaScript Number constructor, properties, and methods list, to know more about the numbers please go through that article.



Last Updated : 23 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads