Return Nearest Greater Integer of Decimal Number in JavaScript



Problem

We are required to write a JavaScript function that lives in the Math class of JavaScript.

Our function should return the nearest greater integer of the decimal number it is being called on.

If the number is already an integer, we should return it as it is.

Example

Following is the code −

 Live Demo

const num = 234.56;
Math.ceil = function(num){
   if(typeof num !== 'number'){
      return NaN;
   };
   if(num % 1 === 0){
      return num;
   };
   const [main] = String(num).split('.');  
   return +main + 1;
};
console.log(Math.ceil(num));

Output

Following is the console output −

235
Updated on: 2021-04-20T08:01:59+05:30

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements