• 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
  • VBA
  • About
You are here: Home / JavaScript / Math floor JavaScript – Using Math.floor() to Round Down to Floor

Math floor JavaScript – Using Math.floor() to Round Down to Floor

December 15, 2021 Leave a Comment

The Math floor JavaScript method will take a number and round it DOWN to the nearest integer. This is done using the Math.floor() method.

var num = Math.floor(1.6);

The above code would round the number 1.6 DOWN to 1.

Some other examples of Math.floor() are below:

var num = Math.floor(1.6);
var num1 = Math.floor(.6);
var num2 = Math.floor(-1.6);
var num3 = Math.floor(100.6);
var num4 = Math.floor(30);

Which would result in the following:

1
0
-2
100
30

Math.floor() in action using jQuery

Below we will provide code to let the user input a number, and then use the Math.floor() method to round the number down. Here is our simple HTML setup:

<p>Type a number you want to use the Math.floor() 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 Math.floor() 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(Math.floor($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.floor($("#userVal").val()));
});

The final code and output for this example is below:

Code Output:

Type a number you want to use the Math.floor() method on below:


Full Code:

<p>Type a number you want to use the Math.floor() 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(Math.floor($("#userVal").val()));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Math.floor($("#userVal").val()));
});

</script>

Hopefully this article has been useful in helping you understand the JavaScript Math.floor() method.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get the Height of an Element
  • 2.  Remove Parentheses From String Using JavaScript
  • 3.  Using JavaScript to Round to 1 Decimal
  • 4.  Using JavaScript to Set Select to the First Option
  • 5.  How to Remove Decimals of a Number in JavaScript
  • 6.  Using JavaScript to Show a Div
  • 7.  Remove Braces from String Using JavaScript
  • 8.  Get the Size of a Set in JavaScript
  • 9.  clearInterval JavaScript – How the clearInterval() Method in JavaScript Works
  • 10.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy