Updated - 22 Jun 20222 mins readPublished : 7 Jun 2022
Published : 7 Jun 2022
The Math.ceil[] function in JavaScript uses the upward rounding method, which accepts a single input, the number to be rounded to the nearest integer. The following is the syntax of ceil in javascript: Math ceil in JavaScript accepts only a single parameter:Overview
Syntax of Math
ceil[] in javascript
Parameters of Math ceil[] in javascript
- value: it is the value to be rounded by the upward rounding method.
Return type of Math ceil[] in javascript
Return type: int
Math ceil in JavaScript returns the number that has been rounded by the upward rounding method or we can say that it returns the smallest integer greater than or equal to the given number.
Exceptions Of Math ceil[] In Javascript
- A non-numeric string passed as parameter returns NaN.
- An array with more than 1 integer passed as parameter returns NaN.
- An empty variable passed as parameter returns NaN.
- An empty string passed as parameter returns NaN.
- An empty array passed as parameter returns NaN.
Example Of Math ceil[] In Javascript
Output :
What Is Math ceil[] In Javascript?
The ceil[] is a JavaScript function that returns the lowest integer value that is greater than or equal to a given number. In other words, the ceil[] method produces an integer value after rounding up a number. Because the ceil[] function is a static function of the Math object, it must be called using the Math placeholder object.
More Examples
Using Math.ceil[] for practical example
console.log[Math.ceil[.95]]; console.log[Math.ceil[7]]; console.log[Math.ceil[6.004]]; console.log[Math.ceil[-0.95]]; console.log[Math.ceil[-9]]; console.log[Math.ceil[-8.004]];
Output :
Explanation: As previously explained, math ceil in JavaScript will convert the integer by the upward rounding method and return that converted value.
Decimal adjustment
// Closure [function[] { function decimalAdjust[type, value, exp] { // If the exp is undefined or zero... if [typeof exp === 'undefined' || +exp === 0] { return Math[type][value]; } value = +value; exp = +exp; } // Decimal ceil if [!Math.ceil10] { Math.ceil10 = function[value, exp] { return decimalAdjust['ceil', value, exp]; }; } }][]; console.log[Math.ceil10[55.51, -1]]; console.log[Math.ceil10[51, 1]]; console.log[Math.ceil10[-75.59, -1]]; console.log[Math.ceil10[-69, 1]];
Output:
The above code is an example of decimal adjustment in javascript.
Browser compatibility
The Math.ceil[] method in javascript is supported by all the browsers such as Chrome, Edge, Safari, Opera etc.
Conclusion
- The Math ceil in JavaScript returns an integer in which the integer is rounded by the upward rounding method.
- As the ceil[] function is a static function of the Math object, it must be called using the Math placeholder object.
See Also
- charAt in JavaScript
- tolowercase in JavaScript
- parseInt.upper[] in Javascript
Challenge Time!
Time to test your skills and win rewards!
Note: Rewards will be credited after the next product update.