0
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.
7

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.