Java 3 / 2 = 1.0?真的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6568847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:48:43  来源:igfitidea点击:

3 / 2 = 1.0? really?

javaandroid

提问by Paul Blaine

Possible Duplicate:
Java Integer Division, How do you produce a double?

可能的重复:
Java 整数除法,你如何产生一个双倍?

double wang = 3 / 2;
Log.v("TEST", "Wang: " + Double.toString(wang));

Logcat output...

Logcat 输出...

07-04 09:01:03.908: VERBOSE/TEST(28432): Wang: 1.0

I'm sure there's an obvious answer to this and probably I'm just tired from coding all night but this has me stumped.

我确信对此有一个明显的答案,可能我只是因为整晚编码而感到疲倦,但这让我难倒。

采纳答案by Tomas Aschan

In many languages, Java being one of them, the way you write a number in an expression decides what type it gets. In Java, a few of the common number types behave like this1:

在许多语言中,Java 就是其中之一,在表达式中写入数字的方式决定了它的类型。在 Java 中,一些常见的数字类型的行为如下1

// In these cases the specs are obviously redundant, since all values will be
// cast correctly anyway, but it was the easiest way to show how to get to the
// different data types :P
int i = 1;
long l = 1L;
float f = 1.0f;   // I believe the f and d for float and double are optional, but
double d = 1.0d;  // I wouldn't bet on what the default is if they're omitted...

Thus, when you declare 3 / 2, you're really saying (the integer 3) / (the integer 2). Java performs the division, and finds the result to be 1(i.e. the integer 1...) since that's the result of dividing 3 and 2 as integers. Finally, the integer 1is cast to the double 1.0dwhich is stored in your variable.

因此,当您声明时3 / 2,您实际上是在说(the integer 3) / (the integer 2)。Java 执行除法,并发现结果是1(即the integer 1...),因为这是将 3 和 2 相除为整数的结果。最后,the integer 1is cast to the double 1.0dwhich 存储在您的变量中。

To work around this, you should (as many others have suggested) instead calculate the quotient of

要解决此问题,您应该(正如许多其他人所建议的那样)改为计算

(the double 3) / (the double 2)

or, in Java syntax,

或者,在 Java 语法中,

double wang = 3.0 / 2.0;


1Source: The Java Tutorialfrom Oracle

1来源:Oracle的 Java 教程

回答by Boris Pavlovi?

Integer division of 3by 2is equal to 1with residue of 1. Casting to doublegives 1.0

3by 的整数除法2等于 的1余数1。铸造double1.0

回答by Eng.Fouad

Try: double wang = 3.0 / 2.0;

尝试: double wang = 3.0 / 2.0;

回答by ChrisWue

3and 2are integer constants and therefore 3 / 2is an integer division which results in 1which is then cast into a double. You want 3.0 / 2.0

32是整数常量,因此3 / 2是一个整数除法,结果1是然后将其转换为双精度数。你要3.0 / 2.0

回答by Mark Allison

That's the expected behaviour. "3" and "2" are both intvalues, and when you perform 3 / 2the result will also be an intvalue which gets rounded down to 1. if you cast both to doublebefore you perform the division then you'll get the result that you expect:

这是预期的行为。"3" 和 "2" 都是int值,当您执行3 / 2结果时,结果也将是一个int向下舍入为 1的值。如果double在执行除法之前将两者都转换为,那么您将得到您期望的结果:

double wang = (double)3 / (double)2;