How to get the smallest integer greater than or equal to a number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

JavaScript for beginners

Best Seller

74 Lectures 10 hours

Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

In this tutorial, we will learn how to get the smallest integer greater than or equal to a number in JavaScript.

To get the smallest integer greater than or equal to a number, use the JavaScript Math.ceil() method. This method returns the smallest integer greater than or equal to a given number. In other words, the Math.ceil() method rounds a number and returns an integer result.

Because the ceil() method is a static function of the Math object, it must be invoked via the Math placeholder object. The ceil() method produces the lowest integer value that is bigger than or equal to a number. The ceil() method is one of the mathematical functions and constants in placeholder object math.

Following are two ways to find the smallest integer greater than or equal to a number in JavaScript.

Using the Math.ceil() Method

Math.ceil() always rounds numbers up to the nearest greatest integer. Because ceil() is a static Math function, it is always used as Math.ceil() instead of using it as a method of a Math object you constructed (here, Math is not a constructor). This function takes only one input. That is the number to be rounded to the closest integer using the upward rounding method.

A non-numeric string, an array containing more than one integer, an empty variable, or an empty string or array supplied as a parameter all yield a NaN. Different browsers, like Mozilla Firefox, Opera, Google Chrome, Internet Explorer, and Safari, all support the Math.ceil() function.

Syntax

let v = Math.ceil(value);


A decimal or integer value is sent as an input to the Math.ceil() function. Then it computes the integer greater than or equal to that number.

Example

In the below example, we compute the smallest integer greater than or equal to a number using Math.ceil() method. We test it for different numbers.

<html>
<body>
<p> Get the smallest integer greater than or equal to a number using
<i> Math.ceil() </i> method </p>
<p id = "result"> </p>
<script>
let result = document.getElementById("result");

let value1 = Math.ceil(89.9);
result.innerHTML = "value1 : "+ value1 + "<br>";

let value2 = Math.ceil(-4.978);
result.innerHTML += "value2 : " + value2 + "<br>";

let value3 = Math.ceil(0.00123);
result.innerHTML += "value3 : " + value3 + "<br>";

let value4 = Math.ceil(-0.0128790);
result.innerHTML += "value4 : "+ value4 + "<br>";

let value5 = Math.ceil("Text.");
result.innerHTML += "value5 : " + value5 + "<br>";
</script>
</body>
</html>


The variable value1 accepts a decimal number as input and returns the nearest rounded-up integer. The variable value2 accepts a negative decimal integer as an input. Then the value3 variable accepts a number close to zero as input and outputs a unit value to the console.

The Math.ceil() function is called with a negative decimal integer, and the result is placed in the value4 variable. Now the value5 variable takes a text as an argument and returns that it is not a number.

Using math.js Library

This solution will use an external contemporary mathematics library known as math.js. This library is used for complicated computational facilities.

The math.js features a complete expression parser with symbolic computing support, a plethora of built-in functions and constants, and an integrated solution for working with diverse data types such as integers, large numbers, complex numbers, fractions, units, and matrices.

It is a huge math library for JavaScript. This is efficient, straightforward, and compatible with JavaScript's built-in Math library. This can run on any JavaScript engine and can be easily extensible. Math.js is an open-source technology.

Syntax

let v = math.ceil(value);


The value is supplied as a parameter to the math.ceil() function to get the smallest integer.

Example

This example shows that various variables are sent to the floor() method, which gives an integer result. The math.js library is included via the script tag.

<html>
<script src="https://unpkg.com/mathjs/lib/browser/math.js"> </script>
<body>
<p> Get the smallest integer greater than or equal to a number using
math.js's <i> math.ceil() </i> method</p>
<p id = "result"> </p>
<script>
let result = document.getElementById("result");

let value1 = math.ceil(36.97);
result.innerHTML = "value1 : "+ value1 + "<br>";

let value2 = math.ceil(-10.9898);
result.innerHTML += "value2 : " + value2 + "<br>";

let value3 = math.ceil(0.1112372);
result.innerHTML += "value3 : " + value3 + "<br>";

let value4 = math.ceil(-91.231);
result.innerHTML += "value4 : "+ value4 + "<br>";

let value5 = math.ceil(1/8);
result.innerHTML += "value5 : " + value5 + "<br>";
</script>
</body>
</html>


In this tutorial, we studied the two strategies used in JavaScript to discover the smallest integer greater than or equal to a number. The first method employs JavaScript's builtin Math library and its ceil() function. The second method is to employ a mathematical library that can be applied to all of the code's mathematical functions.

Updated on 15-Sep-2022 12:24:52