如何在java中获得除法结果的全分数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18554032/
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-12 09:00:14  来源:igfitidea点击:

How to get the full fraction value of a division result in java?

javadrjava

提问by Nair

I am new to Java and I am using DrJava IDE for my testing. I have the following division 49700/40000 and it displays 1.0 instead of 1.2425.

我是 Java 新手,我正在使用 DrJava IDE 进行测试。我有以下分区 49700/40000,它显示 1.0 而不是 1.2425。

 double t = 49700/40000;
 System.out.println(t);
 double t = 49700/40000;
 System.out.println(t);

Is it something I am doing wrong?

这是我做错了吗?

采纳答案by Dev

Try, this instead:

试试,这个:

double t = 49700/40000.0;
System.out.println(t);

If both operands are integers the result will be an integer which will be truncated, and then it will be cast in to a double. If, instead, one of the operands is a double, the result will be a double.

如果两个操作数都是整数,则结果将是一个将被截断的整数,然后将其转换为双精度值。相反,如果其中一个操作数是双精度数,则结果将是双精度数。

回答by Gaurav Varma

Use float for decimal number computations

使用浮点数计算十进制数