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:
How to Iterate Over Characters of a String in JavaScript ?
Next article icon

How to convert string into float in JavaScript?

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below:

Methods to Concert String into Float:

Table of Content

  • Method 1: By using Type Conversion of JavaScript
  • Method 2: By using parseFloat() Method
  • Method 3: By using the eval() function
  • Method 4: By using Number() constructor
  • Method 5: By using Unary Plus Operator


Method 1: By using Type Conversion of JavaScript

In this method, we will use the Type Conversion feature of JavaScript which will convert the string value into float.

Example: Below program demonstrates the above approach 

javascript
// Javascript script // to convert string // to float value  // Function to convert // string to float value function convert_to_float(a) {      // Type conversion     // of string to float     let floatValue = +a;      // Return float value     return floatValue; }  //Driver code let n = "55.225";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +     " Type of " + n + " = " + typeof n);  n = "-33.565";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); 

Output
Converted value = 55.225 Type of 55.225 = number Converted value = -33.565 Type of -33.565 = number  

Method 2: By using parseFloat() Method

In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that 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. 

Example: Below program demonstrates the above approach 

javascript
// Javascript script // to convert string // to float value  // Function to convert // string to float value function convert_to_float(a) {      // Using parseFloat() method     let floatValue = parseFloat(a);      // Return float value     return floatValue; }  //Driver code let n = "245.165";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); n = "-915.55";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); 

Output
Converted value = 245.165 Type of 245.165 = number Converted value = -915.55 Type of -915.55 = number  


Special Case: In French, float numbers are written by the use of a comma (, ) as a separator instead of a dot(.) as a separator.

Example:

The value 245.67 in French is written as 245, 67

To convert a French string into a float in JavaScript we will first use replace() method to replace every (, ) with (.) then follow any of the above-described methods. 

Example: Below program demonstrates the above approach 

javascript
// Javascript script // to convert string // to float value  // Function to convert // string to float value function convert_to_float(a) {     // Using parseFloat() method     // and using replace() method     // to replace ', ' with '.'     let floatValue = parseFloat(a.replace(/, /, "."));      // Return float value     return floatValue; }  //Driver code let n = "245, 165";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n);  n = "-915, 55";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); 

Output
Converted value = 245.165 Type of 245.165 = number Converted value = -915.55 Type of -915.55 = number  

Method 3: By using the eval() function

In this method, we will use the eval() method which is an inbuilt function in JavaScript that is used to evaluate the string return result. If the string contains a number then it converts it from string to number and then returns it and if contains other than the number it returns NaN i.e, not a number. Example: Below program demonstrates the above approach 

JavaScript
// Javascript script // to convert string // to float value  // Function to convert // string to float value function convert_to_float(a) {     // Type conversion     // of string to float     let floatValue = eval(a);      // Return float value     return floatValue; }  //Driver code let n = "55.225";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n);  n = "-33.565";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); 

Output
Converted value = 55.225 Type of 55.225 = number Converted value = -33.565 Type of -33.565 = number  

Method 4: By using Number() constructor

In this method, we can use the Number() constructor which is a built-in function in JavaScript to convert a string into a number, including floats. It parses the argument as a float (or integer) and returns the result.

Example:

JavaScript
// Using Number() constructor to convert a string into a float let stringNumber = "3.14"; let floatNumber = Number(stringNumber); console.log(floatNumber); // Output: 3.14 

Output
3.14 

Method 5: By using Unary Plus Operator

The unary plus (+) operator can be used to convert a string to a number, including floats. It is a simple and concise way to perform this conversion.

Example: Below program demonstrates the above approach

JavaScript
// Javascript script // to convert string // to float value  // Function to convert // string to float value function convert_to_float(a) {     // Using unary plus operator     let floatValue = +a;      // Return float value     return floatValue; }  //Driver code let n = "123.456";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n);  n = "-789.012";  // Call function n = convert_to_float(n);  // Print result console.log("Converted value = " + n +         " Type of " + n + " = " + typeof n); 

Output
Converted value = 123.456 Type of 123.456 = number Converted value = -789.012 Type of -789.012 = number 




Next Article
How to Iterate Over Characters of a String in JavaScript ?

R

Rajnis09
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-DSA
  • JavaScript-Questions
  • javascript-string

Similar Reads

  • How to Get Character of Specific Position using JavaScript ?
    Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1
    4 min read
  • Remove a Character From String in JavaScript
    In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like: Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods
    3 min read
  • Reverse a String in JavaScript
    We have given an input string and the task is to reverse the input string in JavaScript. Using split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characters into a new string, effectiv
    1 min read
  • JavaScript - Convert String to Title Case
    Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript. 1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and
    4 min read
  • JavaScript - Sort an Array of Strings
    Here are the various methods to sort an array of strings in JavaScript 1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values. [GFGTABS] JavaScript let a
    3 min read
  • How to Convert String to Camel Case in JavaScript?
    We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin
    4 min read
  • Extract a Number from a String using JavaScript
    We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console. Below are the methods to extract a number from string using JavaScript: Table of Content Using JavaScript match method with regExUs
    4 min read
  • JavaScript - Delete First Character of a String
    To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common ones Using slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end. [GFGTABS] JavaScript let s1 = "Geeksfor
    1 min read
  • JavaScript - How to Get Character Array from String?
    Here are the various methods to get character array from a string in JavaScript. 1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. [GFGTABS] JavaScript let
    2 min read
  • JavaScript - How To Get The Last Caracter of a String?
    Here are the various approaches to get the last character of a String using JavaScript. 1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1. [GFGTABS] JavaScript const s =
    3 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