Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Number parseInt() Method
Next article icon

JavaScript Number parseFloat() Method

Last Updated : 23 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.



Next Article
JavaScript Number parseInt() Method

V

vivekkothari
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions
  • JavaScript-Numbers

Similar Reads

  • JavaScript Number() Constructor
    Javascript Number() Constructor is used to create a new number object but, if we call it as a function on another data type it will perform type conversion to number if possible. Syntax: Number(object) Parameters: This function accepts a single parameter as mentioned above and described below: objec
    2 min read
  • JavaScript Number constructor Property
    JavaScript Number constructor Property is used to return the number constructor function for the object. The function which is returned by this property is just the reference to this function, not a number containing the function’s name. The JavaScript number constructor, string constructor, and boo
    1 min read
  • JavaScript Number EPSILON Property
    EPSILON property shows the difference between 1 and the smallest floating point number which is greater than 1. When we calculate the value of EPSILON property we found it as 2 to the power -52 (2^-52) which gives us a value of 2.2204460492503130808472633361816E-16. Syntax: Number.EPSILON Attribute:
    2 min read
  • JavaScript Number MAX_SAFE_INTEGER Property
    The JavaScript Number.MAX_SAFE_INTEGER is a constant number that represents the maximum safe integer. This constant has a value of (253 - 1). Here safe refers to the ability to represent integers and to compare them. Syntax: Number.MAX_SAFE_INTEGER Return Value: A constant number. Example 1: Below e
    1 min read
  • JavaScript Number MAX_VALUE & MIN_VALUE Property
    JavaScript Number.MAX_VALUE and Number.MIN_VALUE is used to represent maximum and minimum numeric values in javascript. Syntax: Number.MAX_VALUE Number.MIN_VALUE Return Type: Both Number.MAX_VALUE and Number.MIN_VALUE returns a numeric value. MAX_VALUE has a value of approximately 1.79E+308. The val
    1 min read
  • JavaScript Number.MIN_SAFE_INTEGER Property
    The JavaScript Number.MIN_SAFE_INTEGER is a constant number that represents the minimum safe integer. This constant has a value of (-(253 - 1)). Use Number.MIN_SAFE_INTEGER, as a property of a number object because it is a static property of a number. Syntax: Number.MIN_SAFE_INTEGER Return Value: Co
    1 min read
  • JavaScript NaN Property
    NaN, which stands for "Not a Number," is a special value in JavaScript that shows up when a mathematical operation can't return a valid number. This can happen if you try something like dividing zero by zero or converting a string that doesn't contain numbers. NaN helps you spot when something went
    2 min read
  • JavaScript Number NEGATIVE_INFINITY Property
    In JavaScript, NEGATIVE_INFINITY is a property of a number object which represents negative infinity (-Infinity). It can be explained as a number that is lower than any other number. It is a Read-Only property. Syntax: NEGATIVE_INFINITY is a static property of the NUMBER object and hence is always u
    1 min read
  • JavaScript Number Positive_INFINITY Property
    In javascript Number.POSITIVE_INFINITY represents the largest positive value i.e positive infinity. Actually the value of Number.POSITIVE_INFINITY is the same as the value of the global object's Infinity property. Syntax: It can be assigned to a variable in the following way. let a = Number.POSITIVE
    2 min read
  • JavaScript Number.isNaN() Method
    In JavaScript NaN is stand for Not-a-Number. The Number.isNaN() method returns true if a value is NaN. This method checks whether the passed value is NaN and of type Number, ensuring accurate identification of invalid numeric values. Syntax Number.isNaN(value)Parameters Value: It is the value that i
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences