为什么我无法使用 Int 获取百分比

发布于 2024-08-28 16:39:33 字数 148 浏览 8 评论 0原文

请原谅我的编程知识。我知道这是一件简单的事情,但我不明白为什么结果总是0。为什么十进制就可以了?

int a = 100;
int b = 200;
decimal c = (a / b) * 100;

非常感谢。

Please forgive my programming knowledge. I know this is a simple thing, but I do not understand why result is always 0. Why decimal will be fine?

int a = 100;
int b = 200;
decimal c = (a / b) * 100;

Many thanks.

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

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

发布评论

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

评论(9

月亮坠入山谷 2024-09-04 16:39:33

整数除法总是截断余数。这是在数字被除法时完成的,而不是在将其分配给变量时完成的(正如我猜您所假设的那样)。

decimal c = ((decimal)a / b) * 100;

Integer division always truncates the remainder. This is done at the time that the number is divided, not when it's assigned to the variable (as I'm guessing you assumed).

decimal c = ((decimal)a / b) * 100;
〆凄凉。 2024-09-04 16:39:33

值 a/b 将始终返回 0,因为它们是整数。因此,当评估括号内的值时,从技术上讲,您正在做

decimal c = (0) * 100

更好的事情,

decimal c = ((decimal)a/(decimal)b) * 100

The value a/b will return 0 always since they are integers. So when the values within the Brackets are evaluated you are technically doing this

decimal c = (0) * 100

Better do,

decimal c = ((decimal)a/(decimal)b) * 100
如若梦似彩虹 2024-09-04 16:39:33

100 / 200 是 1/2 或 0.5。由于您使用的是整数,因此它向下舍入为 0(零)。 0 * 100 仍然是 0。括号内的部分总是首先被评估:如果你想让这个工作正常,请使用:

decimal c = (a * 100) / b;

编辑:如果你想要一个更精确的值而不是“整数百分比”(即 33.333%) 33%),你应该使用 Bragaadeesh 的答案。

100 / 200 is 1/2 or 0.5. Since you are using ints, it rounds down to 0 (zero). And 0 * 100 is still 0. The part inside the parentheses is always evaluated first: if you want to get this working, use:

decimal c = (a * 100) / b;

Edit: if you want a more precise value rather than an "integer percentage" (ie. 33.333% instead of 33%), you should use Bragaadeesh's answer.

坏尐絯 2024-09-04 16:39:33

正在执行整数数学运算,结果然后存储为小数。整数数学中的 100 / 200 为 0。要获得百分比,请在计算之前将至少一个值转换为小数。

decimal c = (a / (decimal)b) * 100; 

Integer math is being performed, and the result is then being stored as a decimal. 100 / 200 in integer math is 0. To get your percentage, cast at least one value to decimal before doing the calculation.

decimal c = (a / (decimal)b) * 100; 
别靠近我心 2024-09-04 16:39:33

在强类型语言中,数学运算的结果通常与较大类型相同。

C# 有一个可以执行的隐式数字转换列表。

概括此列表:整型可以转换为浮点类型,但反之则不行。整型也可以隐式转换为十进制,但浮点类型不能。

注意:这也意味着将其中一个整数转换为另一种类型将导致整个答案为该类型。
例如:(十进制)a / b * 100.0 = 50.0

tl;dr:

在 C# 中:

int / int = int
int + decimal = decimal
decimal + int = decimal
int / int * decimal = (int / int = int) * decimal = decimal
int - float = float
int * double = double
float / decimal = an error
int - uint = an error (either that or ulong)

In strongly typed languages, the result of math operations is usually the same type as the larger type.

C# has a list of implicit numeric conversions it will do.

Generalizing this list: Integral types can be converted to floating point types, but not vice versa. Integral types can also be implicitly converted to decimal, but floating point types cannot.

Note: This also means that casting one of the ints to another type will result in the entire answer being that type.
ex: (decimal) a / b * 100.0 = 50.0

tl;dr:

In C#:

int / int = int
int + decimal = decimal
decimal + int = decimal
int / int * decimal = (int / int = int) * decimal = decimal
int - float = float
int * double = double
float / decimal = an error
int - uint = an error (either that or ulong)
姐不稀罕 2024-09-04 16:39:33

正在进行的数学计算仍然是整数数学。

(a/b)(100/200 或 1/2)是对两个整数进行的,因此结果为零。零 * 100 是……好吧,仍然是零。

您遇到的问题是运算顺序(a 和 b 是整数,因此执行整数数学)。

我建议这样做:

decimal c=((decimal)a)/((decimal)b)*100;

这将强制执行数学运算到您寻求的十进制值。

The math being done is still integer math.

(a/b) (100/200 or 1/2) is done on two integers, so the result is zero. Zero * 100 is ... well, still zero.

The problem you are experiencing is with the order of operations (a and b are integers, so integer math is performed).

I suggest this:

decimal c=((decimal)a)/((decimal)b)*100;

This will force the math performed to the decimal values you seek.

云仙小弟 2024-09-04 16:39:33

int 只能是整数,所以 100/200 会是 0。0*100 还是 0。把类型改成小数,就可以了

int can only be whole numbers, so 100/200 is going to be 0. 0*100 is still 0. Change the type to a decimal, and it will work

染柒℉ 2024-09-04 16:39:33

使其十进制 c = (a * 100) / b;

Make it decimal c = (a * 100) / b;

一抹微笑 2024-09-04 16:39:33

使用整数时,“/”代表 DIV,“%”代表 MOD。 DIV 是除法的商,MOD 是余数。因此,100/200 = 0 且 100%200 = 100。

因此,您需要将 ab 类型更改为 decimal,在您的案件。

While using integers, this '/' stands for DIV and this '%' for MOD. DIV is the quotient of the division, MOD is the rest. So, 100/200 = 0 and 100%200 = 100.

So, you need to change a and b types to decimal, in your case.

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