为什么我无法使用 Int 获取百分比
请原谅我的编程知识。我知道这是一件简单的事情,但我不明白为什么结果总是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
整数除法总是截断余数。这是在数字被除法时完成的,而不是在将其分配给变量时完成的(正如我猜您所假设的那样)。
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).
值 a/b 将始终返回 0,因为它们是整数。因此,当评估括号内的值时,从技术上讲,您正在做
更好的事情,
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
Better do,
100 / 200 是 1/2 或 0.5。由于您使用的是整数,因此它向下舍入为 0(零)。 0 * 100 仍然是 0。括号内的部分总是首先被评估:如果你想让这个工作正常,请使用:
编辑:如果你想要一个更精确的值而不是“整数百分比”(即 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:
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.
正在执行整数数学运算,结果然后存储为小数。整数数学中的 100 / 200 为 0。要获得百分比,请在计算之前将至少一个值转换为小数。
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.
在强类型语言中,数学运算的结果通常与较大类型相同。
C# 有一个可以执行的隐式数字转换列表。
概括此列表:整型可以转换为浮点类型,但反之则不行。整型也可以隐式转换为十进制,但浮点类型不能。
注意:这也意味着将其中一个整数转换为另一种类型将导致整个答案为该类型。
例如:
(十进制)a / b * 100.0 = 50.0
tl;dr:
在 C# 中:
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#:
正在进行的数学计算仍然是整数数学。
(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.
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
使其
十进制 c = (a * 100) / b;
Make it
decimal c = (a * 100) / b;
使用整数时,“/”代表 DIV,“%”代表 MOD。 DIV 是除法的商,MOD 是余数。因此,100/200 = 0 且 100%200 = 100。
因此,您需要将
a
和b
类型更改为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
andb
types todecimal
, in your case.