Explain apex division?

280    Asked by ClareMatthews in Salesforce , Asked on Mar 3, 2023
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 an integer block, I got 3 as an 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 it's a perfect division, then I want to leave it as it is.

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.
Answered by Clare Matthews
In order to achieve what you want regarding apex division, you need to round your result up:

Decimal block = a/b;
Integer roundedBlock = block.round(System.RoundingMode.UP));


Your Answer

Interviews

Parent Categories