Integer a = totalRecords.size(); //1250
decimal b = 400;

integer block = a/b;  //3

The actual result of 1250/400 is 3.125. Because I used integer block, I get 3 as answer. Instead of that if I use

decimal block = a/b;  

I get the result as 3.125

But what I want is, if the result is not perfect number (anything with decimals, like 3.125), I want it to be shown as 4 (increment by 1). If its perfect division, then I want to leave it as it it.

How to achieve this? Please let me know.

Another example:

1303/5 = 260.6, I want the result to be 261
1300/5 = 260.  I want 260 only.

2 Answers 2


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.

  • short and very helpful answer, thank you! Commented Mar 29, 2022 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .