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

最后修改: 2022年 8月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

在这个快速教程中,我们将探讨如何从整数除法操作中获得float结果。

2. Introduction to the Problem

2.对问题的介绍

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:

我们可能已经意识到当我们在两个整数上应用除法运算符,如a/b,它总是返回一个整数,即使a不能被b均匀分割,比如。

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.

所以接下来,让我们先了解为什么会发生这种情况,然后想办法得到想要的float结果,例如,本例中的2.5。

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

3.为什么在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.

接下来,让我们看看如果两个整数不能被除,如何得到预期的float结果。

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

4.如何使两个整数的除法结果为浮点数?

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

现在,我们已经了解了除法运算符是如何在两个整数上工作的。JLS还在二进制数字推广中定义了原始操作数的转换规则

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:

根据JLS,当我们计算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. 

根据上面的规则,如果我们想让a/b操作的结果是一个float数,至少有一个操作数应该是float类型。

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

那么,接下来,让我们看看当我们把一个操作数投给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.

如果我们让它运行,上面的测试就会通过。因此,将任何操作数转换为float使得除法产生float结果

5. Conclusion

5.总结

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

在这篇文章中,我们讨论了为什么在Java中整数除法的结果总是整数。

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

此外,我们已经解决了如何从整数除法中获得预期的float结果。这对于不可分割的情况特别有用。

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

像往常一样,文章中介绍的所有代码片段都可以在GitHub上找到