Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert Math.Ceiling result to int?

Tags:

c#

Math.Ceiling returns double because double may store much bigger numbers. However if i'm sure that int type is capable to store the result how should I convert? Is it safe to cast (int) Math.Ceiling(... ?

like image 440
Oleg Vazhnev Avatar asked Jan 12 '12 09:01

Oleg Vazhnev


People also ask

Does Math ceil return an int?

the result is an int , the value of which is a divided by b , rounded toward zero. Because the result is already rounded, ceil() doesn't do anything. Note that this rounding is not the same as floor() , which rounds towards negative infinity.

How do you take integer from Math ceil?

ceil() method with the help of examples. The ceil() method rounds the specified double value upward and returns it. The rounded value will be equal to the mathematical integer. That is, the value 3.24 will be rounded to 4.0 which is equal to integer 4.

What does Math ceiling () mean in C#?

The Math. Ceiling() method in C# is used to return the smallest integral value greater than or equal to the specified number.


1 Answers

If you are sure that you do not cross the capacity of int, it should be perfectly safe to do

int myInt = (int)Math.Ceiling(...); 

If you are not sure about the bound, you could go with long instead of int.

like image 133
Øyvind Bråthen Avatar answered Oct 04 '22 12:10

Øyvind Bråthen