Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

JavaScript - Math.ceil() method example

0 contributions
2 points

The Math.ceil() function returns integer value that is greater than or equal to the argument - result of round up operation. 

// ONLINE-RUNNER:browser;

console.log( Math.ceil(  5     ) ); //  5

console.log( Math.ceil(  2.49  ) ); //  3
console.log( Math.ceil(  2.50  ) ); //  3
console.log( Math.ceil(  2.51  ) ); //  3

console.log( Math.ceil( -2.49  ) ); // -2
console.log( Math.ceil( -2.50  ) ); // -2
console.log( Math.ceil( -2.51  ) ); // -2

console.log( Math.ceil(  0.999 ) ); //  1
console.log( Math.ceil(  1.001 ) ); //  2
console.log( Math.ceil( -1.001 ) ); // -1

1. Documentation

SyntaxMath.ceil(number)
Parametersnumber - integer or float number value (primitive value).
Result

Rounded up number value (primitive value).

If input number value is equal to NaN it returns NaN.

If input  number value is equal to -Infinity it returns -Infinity.

If input  number value is equal to +Infinity it returns +Infinity.

Descriptionceil is static method that takes only one parameter and return rounded up value.

2. Rounding with precision up-to n places example

// ONLINE-RUNNER:browser;

function ceilPrecised(number, precision) {
	var power = Math.pow(10, precision);

  	return Math.ceil(number * power) / power;
}

// Example:

console.log( ceilPrecised(     5  ,  0 ) ); // 5
console.log( ceilPrecised(     5. ,  0 ) ); // 5
console.log( ceilPrecised(      .5,  0 ) ); // 1

console.log( ceilPrecised(  1.1234,  0 ) ); // 2
console.log( ceilPrecised(  1.1234,  1 ) ); // 1.2
console.log( ceilPrecised(  1.1235,  2 ) ); // 1.13
console.log( ceilPrecised(  1.1235,  3 ) ); // 1.124

console.log( ceilPrecised( -1.1234,  0 ) ); // -1
console.log( ceilPrecised( -1.1234,  1 ) ); // -1.1
console.log( ceilPrecised( -1.1234,  2 ) ); // -1.12
console.log( ceilPrecised( -1.1234,  3 ) ); // -1.123
 
console.log( ceilPrecised(    1234, -1 ) ); // 1240
console.log( ceilPrecised(    1234, -2 ) ); // 1300
console.log( ceilPrecised(    1234, -3 ) ); // 2000

console.log( ceilPrecised(  5_000.000_001,  0 ) ); // 5001
console.log( ceilPrecised(  5_000.000_001,  6 ) ); // 5000.000001
console.log( ceilPrecised(  5_000.000_001, -3 ) ); // 6000

References

  1. Floor and ceiling functions - Wikipedia

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more