问题
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