Java中的整数除法
这是一个基本问题,但我找不到答案。我研究了浮点算术和其他一些主题,但似乎没有任何内容可以解决这个问题。我确信我只是用错了术语。
基本上,我想获取两个数量 - 已完成的数量和总计 - 并将它们除以得出百分比(已完成的数量)。数量为long
。设置如下:
long completed = 25000;
long total = 50000;
System.out.println(completed/total); // Prints 0
我尝试将结果重新分配给 double - 它打印 0.0
。我哪里错了?
顺便说一句,下一步是将这个结果乘以 100,我认为一旦跨过这个小障碍,这应该很容易。
顺便说一句,这里不是家庭作业,只是普通的老笨蛋(也许今天编码太多)。
This is a basic question but I can't find an answer. I've looked into floating point arithmetic and a few other topics but nothing has seemed to address this. I'm sure I just have the wrong terminology.
Basically, I want to take two quantities - completed, and total - and divide them to come up with a percentage (of how much has been completed). The quantities are long
s. Here's the setup:
long completed = 25000;
long total = 50000;
System.out.println(completed/total); // Prints 0
I've tried reassigning the result to a double - it prints 0.0
. Where am I going wrong?
Incidentally, the next step is to multiply this result by 100, which I assume should be easy once this small hurdle is stepped over.
BTW not homework here just plain old numskull-ness (and maybe too much coding today).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
转换输出为时已晚;计算已经在整数算术中进行了。您需要将输入转换为
double
:请注意,您实际上不需要转换两个输入。只要其中一个是
double
,另一个就会被隐式转换。但为了对称,我更喜欢两者都做。Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to
double
: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.为此你甚至不需要双打。只需先乘以 100,然后除即可。否则结果将小于 1 并被截断为零,如您所见。
编辑:或者如果可能溢出,如果会溢出(即被除数大于922337203685477581),请先将除数除以100。
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.
只需键入强制转换其中任何一个即可。
Just type cast either of them.
将
completed
和total
都转换为double
或在进行除法时至少将它们转换为double
。即,将变量转换为双倍,而不仅仅是结果。公平警告,使用
时存在浮点精度问题浮动
和双精度
。Convert both
completed
andtotal
todouble
or at least cast them todouble
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
anddouble
.如果在进行除法之前没有显式地将两个值之一转换为浮点数,则将使用整数除法(这就是为什么你得到 0)。您只需要两个操作数之一是浮点值,以便使用正常除法(其他整数值会自动转换为浮点值)。
只要尝试一下
就可以了。
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
and it will be fine.
正如 JLS< 所解释的/a>,整数运算非常简单。
因此,简单来说,操作总是会产生
int
值,唯一的例外是其中包含long
值。请注意,运算符的优先级很重要,因此如果您进行
int * int
运算会产生int
,它将被提升为long< /code> 运算过程中
int + long
As explain by the JLS, integer operation are quite simple.
So to make it short, an operation would always result in a
int
at the only exception that there is along
value in it.Note that the priority of the operator is important, so if you have
the
int * int
operation would result in anint
, it would be promote into along
during the operationint + 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: