• 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 / JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling

JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling

December 17, 2021 Leave a Comment

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

var num = Math.ceil(1.6);

The above code would round the number 1.6 UP to 2.

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

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

Which would result in the following:

2
1
-1
101
30

Math.ceil() in action using jQuery

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

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

The final code and output for this example are below:

Code Output:

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


Full Code:

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Add Minutes to a Date Using JavaScript
  • 2.  Using JavaScript to Change the Image src
  • 3.  Using Javascript to Remove Class from Element
  • 4.  Using JavaScript to Set the Cursor Position
  • 5.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 6.  Grouping By Multiple Properties and Summing Using Javascript
  • 7.  Remove the Last Element From Array in JavaScript
  • 8.  Using JavaScript to Check if a Number is Divisible by 2
  • 9.  Using JavaScript to Get the Min Value in an Array
  • 10.  Using JavaScript to Check if Variable is a Function

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