Java中的整数除法

发布于 2024-12-01 22:19:56 字数 424 浏览 1 评论 0原文

这是一个基本问题,但我找不到答案。我研究了浮点算术和其他一些主题,但似乎没有任何内容可以解决这个问题。我确信我只是用错了术语。

基本上,我想获取两个数量 - 已完成的数量和总计 - 并将它们除以得出百分比(已完成的数量)。数量为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 longs. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

摇划花蜜的午后 2024-12-08 22:19:56

转换输出为时已晚;计算已经在整数算术中进行了。您需要将输入转换为double

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

请注意,您实际上不需要转换两个输入。只要其中一个是double,另一个就会被隐式转换。但为了对称,我更喜欢两者都做。

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.

遗失的美好 2024-12-08 22:19:56

为此你甚至不需要双打。只需先乘以 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.

青衫负雪 2024-12-08 22:19:56
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

只需键入强制转换其中任何一个即可。

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.

最终幸福 2024-12-08 22:19:56

completedtotal 都转换为 double 或在进行除法时至少将它们转换为 double。即,将变量转换为双倍,而不仅仅是结果。

公平警告,使用 时存在浮点精度问题浮动双精度

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.

甩你一脸翔 2024-12-08 22:19:56

如果在进行除法之前没有显式地将两个值之一转换为浮点数,则将使用整数除法(这就是为什么你得到 0)。您只需要两个操作数之一是浮点值,以便使用正常除法(其他整数值会自动转换为浮点值)。

只要尝试一下

float completed = 50000.0f;

就可以了。

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.

记忆里有你的影子 2024-12-08 22:19:56

正如 JLS< 所解释的/a>,整数运算非常简单。

如果除移位运算符之外的整数运算符至少有一个 long 类型的操作数,则使用 64 位精度进行运算,并且数值运算符的结果为 long 类型。如果另一个操作数不长,则首先将其加宽(第 5.1.5 节),通过数字提升(第 5.6 节)键入长整型。

否则,使用32位精度进行运算,数值运算符的结果为int类型。如果任一操作数不是 int,则首先通过数字提升将其扩展为 int 类型。

因此,简单来说,操作总是会产生 int 值,唯一的例外是其中包含 long 值。

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

请注意,运算符的优先级很重要,因此如果您进行

long = int * int + long

int * int 运算会产生 int,它将被提升为 long< /code> 运算过程中 int + long

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

木森分化 2024-12-08 22:19:56

当你的输出结果是双精度时,你应该在除法时将完成的变量或总变量或两者都转换为双精度。

因此,正确的实现是:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文