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.

## Leave a Reply