Java 中的整数除法

这是一个基本问题,但我找不到答案。我研究了浮点数算法和其他一些主题,但似乎没有解决这个问题。我肯定我用错术语了。

基本上,我想采取两个数量-完成,和总-和除以他们拿出一个百分比(多少已经完成)。数量是 long。这是设置:

long completed = 25000;
long total = 50000;


System.out.println(completed/total);  // Prints 0

我已经尝试将结果重新分配给一个双重打印 0.0。我哪里出错了?

顺便说一下,下一步是将这个结果乘以100,我想一旦跨过这个小障碍,这应该很容易。

顺便说一句,这里没有家庭作业,只是一个老掉牙的笨蛋(也许今天编写了太多代码)。

257502 次浏览

Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double:

System.out.println((double)completed/(double)total);

Note that you don't actually need to convert both of the inputs. So long as one of them is double, the other will be implicitly converted. But I prefer to do both, for symmetry.

Convert both completed and total to double or at least cast them to double when doing the devision. I.e. cast the varaibles to double not just the result.

Fair warning, there is a floating point precision problem when working with float and double.

If you don't explicitly cast one of the two values to a float before doing the division then an integer division will be used (so that's why you get 0). You just need one of the two operands to be a floating point value, so that the normal division is used (and other integer value is automatically turned into a float).

Just try with

float completed = 50000.0f;

and it will be fine.

You don't even need doubles for this. Just multiply by 100 first and then divide. Otherwise the result would be less than 1 and get truncated to zero, as you saw.

edit: or if overflow is likely, if it would overflow (ie the dividend is bigger than 922337203685477581), divide the divisor by 100 first.

In Java
Integer/Integer = Integer
Integer/Double = Double//Either of numerator or denominator must be floating point number
1/10 = 0
1.0/10 = 0.1
1/10.0 = 0.1

Just type cast either of them.

As explain by the JLS, integer operation are quite simple.

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).

Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.

So to make it short, an operation would always result in a int at the only exception that there is a long value in it.

int = int + int
long = int + long
int = short + short

Note that the priority of the operator is important, so if you have

long = int * int + long

the int * int operation would result in an int, it would be promote into a long during the operation int + long

As your output results a double you should cast either completed variable or total variable or both to double while dividing.

So, the correct implmentation will be:

System.out.println((double)completed/total);