• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Convert String to Float in JavaScript

Convert String to Float in JavaScript

September 15, 2022 Leave a Comment

We can convert a string to float in JavaScript by using the JavaScript parseFloat() method.

var num = parseFloat("20.12345");

The above code would return the number 20.12345.


The parseFloat() JavaScript method will parse the value given to it as a string and then return the first Float in the string. If no number is found at the start, NaN will be returned.

Some other examples of the parseFloat() method are below:

var num1 = parseFloat("$11");
var num2 = parseFloat("30 40 50");
var num3 = parseFloat("The first number is 2 followed by 3");
var num4 = parseFloat("10.10 is the first number");
var num5 = parseFloat("-10.59 50.50");

console.log(num1);
console.log(num2);
console.log(num3);
console.log(num4);
console.log(num5);

#Output
NaN
30
NaN
10.1
-10.59

Note the parseFloat() JavaScript method is similar to the parseInt() method, the main difference being that parseInt() will only return the Integer, while parseFloat() will return decimal values as well.

Convert String to Float using JavaScript

Below we will provide code to let the user input a string, and then use the parseFloat() method to return the first number. Here is our simple HTML setup:

<p>Type something you want to use the parseFloat() method on below:</p>
<input id="userVal" type="text" value="">
<div id="click-me"  onclick="convertStringToFloat()">Convert to Float</div>
<div id="results"></div>

Below is the JavaScript code which takes the user input using an onclick event, and uses the parseFloat() method on that user input. We update the results below using the textContent property.

function convertStringToFloat(){
  document.getElementById("results").textContent = parseFloat(document.getElementById("userVal").value);
};

The final code and output for this example is below:

Code Output:

Type something you want to use the parseFloat() method on below:

Convert to Float

Full Code:

<p>Type something you want to use the parseFloat() method on below:</p>
<input id="userVal" type="text" value="">
<div id="click-me"  onclick="convertStringToFloat()">Convert to Float</div>
<div id="results"></div>

<script>

function convertStringToFloat(){
  document.getElementById("results").textContent = parseFloat(document.getElementById("userVal").value);
};

</script>

Hopefully this article has been useful in helping you understand how to convert a string to float in JavaScript

Other Articles You'll Also Like:

  • 1.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 2.  How to Call a JavaScript Function From HTML
  • 3.  Using JavaScript to Remove Character From String
  • 4.  Using JavaScript to Subtract Days From a Date
  • 5.  Creating a JavaScript Function to Subtract Two Numbers
  • 6.  How to Remove Decimals of a Number in JavaScript
  • 7.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 8.  Using JavaScript to Change the Font Size of a Paragraph
  • 9.  Using JavaScript to Scroll to Bottom of Div
  • 10.  JavaScript substring – How to Get a Substring From a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy