This is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).
system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);
system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);
system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);
If you know you have Integers and you want to get their ceiling, you could do something like:
public static Integer ceiling(Integer x, Integer y)
{
return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
Answer from Adrian Larson on Stack ExchangeThis is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).
system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);
system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);
system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);
If you know you have Integers and you want to get their ceiling, you could do something like:
public static Integer ceiling(Integer x, Integer y)
{
return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
Integer Division
In Apex Code, similar to Java, when there are two like numeric data types (e.g. both Integers), then they are calculated in a way that returns the same data type. Integers cannot store fractions, so when you do something like 7/3, the fraction is silently discarded. As far as I know, integer division works in the same for every programming language in the world where the result of integer arithmetic is an integer.In many languages where the result of two integers being divided together results in an integer, the fraction is often dropped entirely, although exceptions do exist (some perform rounding, instead).
Parameter Promotion
You'll notice that there's no function Math.ceil that accepts an Integer. This means if you give it an integer, it will implicitly be cast to a floating point value before being processed by Math.ceil.
Arithmetic Promotion
When two numbers are operated on using the standard operators (+, -, /, *), if one of those parameters are a floating point value, the other one will also automatically become a floating point value. Similarly, if a integer and a long were involved in an operation, both numbers become long, and the return type becomes long. So, the goal is to create a situation where a floating point is returned. For example, this results in the correct result:
Integer x = Math.ceil(7.0/3).intValue();
Here, the 7.0 indicates a floating point operation. You'd also get the same effect if you did this:
Decimal x = 7;
Integer y = 3;
Integer z = Math.ceil(x/y).intValue();
This behavior is well-defined, and mimics the behavior in Java. You can read more about automatic widening conversion in the Java documentation, as well as rules about integer division. You'll find that, while not explicitly mentioned in the Apex Code Developer's Guide (as far as I can tell), it obeys the same rules.
See documentation for Decimal class
in order to achieve what you want you need to round your result up:
Decimal block = a/b;
Integer roundedBlock = block.round(System.RoundingMode.UP));
UPDATE: I have suggested you use RoundingMode.UP based on your examples, however you should also consider using RoundMode.CEILING (or Math.ceil() as suggested by @MLucci) - difference between the two is around behaviour with negative numbers.
You can try this code : - The solution is to explicitly convert one of the values in the division expression to a Double before the division operation is performed.
Integer x = 5;
Integer y = 2;
Double d = ((Double) x) / y;
In this case the Integer x is casted to a Double and the Integer y is promoted to a Double for the duration of the calculation, so that a Double division can be performed. The result is 2.5.