0

round up/round down?

i got to this question which asks for the value of the variable result: int x = 15; int y = 4; int result = x / y; System.out.println(result); 15 divided by 4 is 3.75 and i understand that because the variable result is an integer the answer will be a single figure as an integer is a whole number, what i don't understand is why is the answer rounded down to 3 instead of rounded up to 4? can someone shed some light please?

21st Sep 2016, 1:40 PM
gaz
1 Resposta
0
A good way to round down is Math.floor() it goes like this in your case. import java.lang.Math; //Make sure you put this on top of everything int x=15; int y =4; int result = x/y; double result2 = Math.floor(result); System.out.println(result2); Hope this helps!
21st Sep 2016, 7:08 PM
Cosmiverse
Cosmiverse - avatar