• 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 / parseFloat JavaScript – How to use the JavaScript parseFloat() method

parseFloat JavaScript – How to use the JavaScript parseFloat() method

January 21, 2022 Leave a Comment

The parseFloat JavaScript method will parse the value given to it as a string and then return the first number in the string. This is done using the parseFloat() method.

var num = parseFloat("20");

The above code would return the number 20.

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

var num1 = parseFloat(1);
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 50");

Which would result in the following:

1
30
NaN
10.1
-10

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

parseFloat() in action using jQuery

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

<p>Type a number or string you want to use the parseFloat() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

Below is the JavaScript and jQuery code which take the user input using the jQuery click() or on() keypress methods, and use the parseFloat() method on that user input and update the results below using the jQuery text() method.

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(parseFloat($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(parseFloat($("#userVal").val()));
});

The final code and output for this example is below:

Code Output:

Type a number or string you want to use the parseFloat() method on below:


Full Code:

<p>Type a number or string you want to use the parseFloat() method on below:</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" value="Submit">
<div id="results"></div>

<script>

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(parseFloat($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(parseFloat($("#userVal").val()));
});

</script>

Hopefully this article has been useful in helping you understand the parseFloat JavaScript method.

Other Articles You'll Also Like:

  • 1.  Remove Empty Strings from an Array in JavaScript
  • 2.  Remove Special Characters From String in JavaScript
  • 3.  Using the getElementsByClassName Method in JavaScript
  • 4.  Using Javascript to Check if Value is Not Null
  • 5.  JavaScript value – Get the Value from an Input Field
  • 6.  How to Check if a String Contains Vowels in JavaScript
  • 7.  Using JavaScript to Remove Substring From String
  • 8.  JavaScript Change Background Color of Element
  • 9.  JavaScript String endsWith Method
  • 10.  JavaScript Random Boolean – How to Generate Random Boolean Values

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