you are casting an the result of integer division to a double.
You need to cast each part of the division to double BEFORE the result.
double pages = (double)total/(double)perPage;
The rest should work
Answer from Adam Yost on Stack Overflowyou are casting an the result of integer division to a double.
You need to cast each part of the division to double BEFORE the result.
double pages = (double)total/(double)perPage;
The rest should work
(int)Math.ceil(3/2.0) will give answer 2
(int)Math.ceil(3/2) will give answer 1
In order to get the float value, you need to cast (or add .0) to one of the arguments
Math.ceil() returns the ceiled value. It can't change the value of the variable it takes as argument, because Java passes arguments by value. So you need to do
hours = Math.ceil(hours);
The actual solution is to use double inside the ceil method.
Math.ceil(7 * 50 / 100) will return 3.0 even though the actual value resulting from 7*50/100 is 3.5. It is because since everything is int, the result of 350/100 itself will be 3.
If however, if you give Math.ceil(7 * 50 / 100D), the result would be 4.0.
So, the 4.999 in your question should be a double and not a result of an integer operation like 4999/1000.
Just make sure that whatever you give inside a the ceil is double and not an int.
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));
}
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.
You are doing 157/32 which is dividing two integers with each other, which always result in a rounded down integer. Therefore the (int) Math.ceil(...) isn't doing anything. There are three possible solutions to achieve what you want. I recommend using either option 1 or option 2. Please do NOT use option 0.
Option 0
Convert a and b to a double, and you can use the division and Math.ceil as you wanted it to work. However I strongly discourage the use of this approach, because double division can be imprecise. To read more about imprecision of doubles see this question.
int n = (int) Math.ceil((double) a / b));
Option 1
int n = a / b + ((a % b == 0) ? 0 : 1);
You do a / b with always floor if a and b are both integers. Then you have an inline if-statement which checks whether or not you should ceil instead of floor. So +1 or +0, if there is a remainder with the division you need +1. a % b == 0 checks for the remainder.
Option 2
This option is very short, but maybe for some less intuitive. I think this less intuitive approach would be faster than the double division and comparison approach:
Please note that this doesn't work for b < 0.
int n = (a + b - 1) / b;
To reduce the chance of overflow you could use the following. However please note that it doesn't work for a = 0 and b < 1.
int n = (a - 1) / b + 1;
Explanation behind the "less intuitive approach"
Since dividing two integers in Java (and most other programming languages) will always floor the result. So:
int a, b;
int result = a/b (is the same as floor(a/b) )
But we don't want floor(a/b), but ceil(a/b), and using the definitions and plots from Wikipedia: 
With these plots of the floor and ceil functions, you can see the relationship.

You can see that floor(x) <= ceil(x). We need floor(x + s) = ceil(x). So we need to find s. If we take 1/2 <= s < 1 it will be just right (try some numbers and you will see it does, I find it hard myself to prove this). And 1/2 <= (b-1) / b < 1, so
ceil(a/b) = floor(a/b + s)
= floor(a/b + (b-1)/b)
= floor( (a+b-1)/b) )
This is not a real proof, but I hope you're satisfied with it. If someone can explain it better I would appreciate it too. Maybe ask it on MathOverflow.
157/32 is int/int, which results in an int.
Try using the double literal - 157/32d, which is int/double, which results in a double.
You need to change this:
num = Math.ceil(num);
What's happening is you are not assigning the value from Math.ceil(num) to anything.
You aren't assigning the value of Math.ceil. The value of num remains unchanged.
To print the ceiling of num you can either put the call in sys out or just try following code:
double num = 0.4;
System.out.println(num); // 0.4
double ceiledNum=Math.ceil(num);
System.out.println(ceiledNum);