So I'm working on a Java project, and I type this:
double totalPages;
int amountKits = 2;
totalPages = amountKits / 3;
System.out.println("Total pages before ceil: " + totalPages);
totalPages = Math.ceil(totalPages);
System.out.println("Amount of Kits: " + amountKits);
System.out.println("Total Pages: " + totalPages);
And here is the result:
Total pages before ceil: 0.0
Amount of Kits: 2
Total Pages: 0.0
Does anyone know why this is happening? (Sorry, I'm a noob, I know)
Because you're calculating 2 / 3
in integer arithmetic.
Try:
totalPages = amountKits / 3.0;
instead.
2/3
is integer division that returns 0. This then gets converted to a floating point value, 0.0 when you assign to totalPages
.
You need to perform floating point division, for example by writing amountKits/3.0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.