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

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:32:21

问题


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.

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

Is it something I am doing wrong?


回答1:


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.




回答2:


Use float for decimal number computations



来源:https://stackoverflow.com/questions/18554032/how-to-get-the-full-fraction-value-of-a-division-result-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!