I need to perform division between integers in Java, and the result should be a float.
Could I just use /
symbol for it? As in:
int integer1 = 1;
int integer2 = 2;
float quotient = integer1 / integer2; // Could I do this?
When dividing two integers, Java uses integer division. In integer division, the result is also an integer. The result is truncated (the fractional part is thrown away) and not rounded to the closest integer.
The Math. floorDiv() method divides one integer ( int or long ) by another, and rounds the result down to the nearest integer value. If the result is positive, the effect is the same as using the Java / division operator described earlier in this text.
Cast one of the integers to float to ensure a floating point division:
float result = integer1 / (float) integer2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With