Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Math.ceil not rounding upwards?

Tags:

java

math

ceil

I have the following code:

int total = 6;
int perPage = 5;
double pages = total/perPage;
double ceilPages = Math.ceil(pages);
out.println(ceilPages);

Which outputs 1.0.

I thought it should output 2.0 because the result of total/perPage is 1.2.

Why is it not rounding upwards to 2.0?

like image 645
crmepham Avatar asked Jun 05 '14 14:06

crmepham


1 Answers

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

like image 118
Adam Yost Avatar answered Sep 25 '22 06:09

Adam Yost