数据库单元格值的计算

发布于 2024-12-27 02:44:22 字数 254 浏览 0 评论 0原文

我有一个带有表的数据库,其中包含两个整数字段。

当我尝试获取百分比 (fieldA / (fieldA+fieldB)) 时,该值为 0。

double percentage = fieldA+fieldB // WORKS; 5+5=10
double percentage = fieldA / (fieldA+fieldB) // DOES NOT WORK; 5+5=0

那么这里有什么问题呢?谢谢..

I have a database with a table, containing two integer fields.

When I try to get a percentage (fieldA / (fieldA+fieldB)), the value is 0.

double percentage = fieldA+fieldB // WORKS; 5+5=10
double percentage = fieldA / (fieldA+fieldB) // DOES NOT WORK; 5+5=0

So what's the problem here? Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

失与倦" 2025-01-03 02:44:22

当您执行 fieldA / (fieldA+fieldB) 时,您会得到 5/10,作为整数,它会被截断为 0,如果您想要结果为 0.5,则必须进行双除。
即像这样:

double percentage = (double)fieldA/(fieldA+fieldB)

When you do fieldA / (fieldA+fieldB) you get 5/10, which as an integer is truncated to 0, you have to do double division if you want 0.5 as a result.
i.e. something like this:

double percentage = (double)fieldA/(fieldA+fieldB)
久隐师 2025-01-03 02:44:22

我确实假设 fieldAfieldB 是整数?如果是,您应该知道,无论等式左边是什么,两个整数相除也会得到一个整数。

因此,将 fieldA 除以 (fieldA+fieldB) 会得到一个值 (fieldA+fieldB)。 1 结果为零。请参阅 Wikipedia 上的整数除法

要解决此问题,只需将至少一个操作数转换为浮点类型,例如:

double percentage = fieldA/(double)(fieldA+fieldB)

I do assume fieldA and fieldB are integers? If yes, you should be aware that dividing two integers results in an integer, too, no matter what is on the left side of the equation.

So dividing fieldA by (fieldA+fieldB) results in a value < 1 which results in zero. See Integer Division on Wikipedia.

To correct the issue, simply cast at least one operand to a floating point type like e.g.:

double percentage = fieldA/(double)(fieldA+fieldB)
玉环 2025-01-03 02:44:22

由于 fieldA 和 fieldB 是整数,因此表达式 fieldA / (fieldA+fieldB) 的形式为 int / int,这意味着您将使用整数除法,在本例中 - < code>5/10 = 0,因为整数除法求解 x = am + b,在本例中 5 = a*10 + b 这意味着 <代码>a = 0, b = 5

但是您可以这样做:

double percentage = (double)fieldA / (fieldA+fieldB);

Since fieldA and fieldB are integers, the expression fieldA / (fieldA+fieldB)is of the form int / int which means you will use integer division, in this case - 5/10 = 0, as integer division solves x = am + b, and in this case 5 = a*10 + b which means a = 0, b = 5

You can do however:

double percentage = (double)fieldA / (fieldA+fieldB);
長街聽風 2025-01-03 02:44:22

在应用操作之前,您可能需要将字段转换为双精度型

You may need to cast the fields as doubles before applying the operation

月光色 2025-01-03 02:44:22

试试这个:双倍百分比 = (双)fieldA/(double)(fieldA+fieldB)

try this : double percentage = (double)fieldA/(double)(fieldA+fieldB)

岁月如刀 2025-01-03 02:44:22

如果两个字段都是整数,则两个整数相加的结果也是整数。
试试这个

浮点结果 = (float)((A*100)/(B+A));

答案:结果 = 50.0

if both fields are integer, then the addition of the two integer results also integer.
Try this

float result = (float)((A*100)/(B+A));

Answer: result = 50.0

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