Make Division of Two Integers Result in a Float – 使两个整数的除法结果为浮点数

最后修改: 2022年 8月 30日

1. Overview


We can use the division operator (/) to divide the left-hand value by the right-hand one in Java. For instance, int number = 10 / 5.

我们可以使用除法运算符(/),在Java中用左边的值除以右边的值。例如,int number = 10 / 5

In this quick tutorial, we’ll explore how to get a float result from the integer division operation.


2. Introduction to the Problem


First of all, for simplicity, we’ll use unit test assertions to verify the calculation result in this tutorial.


We may have realized when we apply the division operator on two integers like a/b, it always returns an integer, even though a isn’t evenly divisible by b, for example:


int i = 10 / 4;
assertEquals(2, i);

If we run the test, it passes. So, 10 / 4 produces 2 instead of 2.5. Moreover, even if we assign the calculation result to a float variable, the result is still two:

如果我们运行这个测试,它就会通过。所以,10 / 4 产生2而不是2.5。此外,即使我们将计算结果分配给一个float变量,其结果仍然是2。

float x = 10 / 4;
assertEquals(2, x);

So next, let’s first understand why this happens and then figure out how to get the desired float result, for example, 2.5 in this case.


3. Why Does the Division of Two Integers Always Result in an Integer in Java?


To understand why 10 / 4 = 2 in Java, we need to check the division operator section in JLS (Java Language Specification) to know how the division operator’s behavior is defined.

为了理解为什么在Java中10 / 4 = 2,我们需要查看JLS(Java语言规范)中的除法运算符部分,了解除法运算符的行为是如何定义的。

First, the specification states that integer division rounds toward zero. In other words, the result of the division operation of two integers is only the quotient value. The remainder is not included.


Therefore, when we calculate 10 / 4, we get 2 instead of 2.5.

因此,当我们计算10 / 4时,我们得到2而不是2.5。

Next, let’s see how to get the expected float result if the two integers are not divisible.


4. How to Make Division of Two Integers Result in a Float?


Now, we’ve understood how the division operator works with two integers. JLS has also defined primitive operands conversion rules in Binary numeric promotion.


Binary numeric promotion is performed on *, /, and % operators. Therefore, the division operation follows the operand conversion rules.


So next, let’s take a closer look at the operand conversion rule.


As per the JLS, when we calculate a/b:


  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

Based on the rule above, if we want to make the a/b operation result in a float number, at least one operand should be of type float. 


So, next, let’s see if we can get the expected result when we cast one operand to float:


float x = (float) 10 / 4;
assertEquals(2.5, x);

float y = 10 / (float) 8;
assertEquals(1.25, y);

The test above passes if we give it a run. Therefore, casting any operand to float makes the division produce a float result.


5. Conclusion


In this quick article, we’ve discussed why the division of integers always results in integers in Java.


Further, we’ve addressed how to get the expected float result from the division of integers. This is particularly useful for nondivisible cases.


As usual, all code snippets presented in the article are available over on GitHub.