为什么 Decimal.Divide(int, int) 有效,但 (int / int) 无效?
为什么将两个 32 位 int 数字除以 ( int / int ) 会返回 0
,但如果我使用 Decimal.Divide()
我会得到正确的答案? 我绝不是一个ac#家伙。
How come dividing two 32 bit int numbers as ( int / int ) returns to me 0
, but if I use Decimal.Divide()
I get the correct answer? I'm by no means a c# guy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
int
是整数类型; 两个整数相除执行整数除法,即小数部分被截断,因为它无法存储在结果类型中(也是int
!)。 相比之下,Decimal
有小数部分。 通过调用Decimal.Divide
,您的int
参数将隐式转换为Decimal
。您可以通过将至少一个参数显式转换为浮点类型来对 int 参数强制执行非整数除法,例如:
int
is an integer type; dividing two ints performs an integer division, i.e. the fractional part is truncated since it can't be stored in the result type (alsoint
!).Decimal
, by contrast, has got a fractional part. By invokingDecimal.Divide
, yourint
arguments get implicitly converted toDecimal
s.You can enforce non-integer division on
int
arguments by explicitly casting at least one of the arguments to a floating-point type, e.g.:在第一种情况下,您正在进行整数除法,因此结果被截断(小数部分被截掉)并返回一个整数。
第二种情况,首先将整数转换为小数,结果是小数。 因此它们不会被截断,您会得到正确的结果。
In the first case, you're doing integer division, so the result is truncated (the decimal part is chopped off) and an integer is returned.
In the second case, the ints are converted to decimals first, and the result is a decimal. Hence they are not truncated and you get the correct result.
以下行:
...将使用整数算术来执行。 另一方面,
Decimal.Divide
采用两个Decimal
类型的参数,因此将对十进制值而不是整数值执行除法。 这相当于:要检查这一点,您可以在上述每个示例之后添加以下代码行:
第一种情况的输出将为
..,第二种情况的输出为:
The following line:
...will be performed using integer arithmetic.
Decimal.Divide
on the other hand takes two parameters of the typeDecimal
, so the division will be performed on decimal values rather than integer values. That is equivalent of this:To examine this, you can add the following code lines after each of the above examples:
The output in the first case will be
..and in the second case:
我认为 Decimal.Divide(decimal,decimal) 在返回十进制值(精确)之前隐式将其 2 个 int 参数转换为小数,其中 4/5 被视为整数除法并返回 0
I reckon
Decimal.Divide(decimal, decimal)
implicitly converts its 2 int arguments to decimals before returning a decimal value (precise) where as 4/5 is treated as integer division and returns 0您想要转换数字:
double c = (double)a/(double)b;
注意:如果 C# 中的任何参数是双精度型,则使用双除法得到双精度型。 因此,以下也可以:
double c = (double)a/b;
这是一个小程序:
You want to cast the numbers:
double c = (double)a/(double)b;
Note: If any of the arguments in C# is a double, a double divide is used which results in a double. So, the following would work too:
double c = (double)a/b;
here is a Small Program :
就我而言,上面没有任何作用。
我想要做的是将 278 除以 575,然后乘以 100 得出百分比。
%: 48,3478260869565 --> 278 / 575 ---> 0
%: 51,6521739130435 --> 297 / 575 ---> 0
如果我将 PeopleCount 乘以 1.0,它会变成十进制,除法将是 48.34...
也乘以 100.0 而不是 100。
In my case nothing worked above.
what I want to do is divide 278 by 575 and multiply by 100 to find percentage.
%: 48,3478260869565 --> 278 / 575 ---> 0
%: 51,6521739130435 --> 297 / 575 ---> 0
if I multiply the PeopleCount by 1.0 it makes it decimal and division will be 48.34...
also multiply by 100.0 not 100.
如果您正在寻找 0 < 一个< 1 个答案,int / int 是不够的。 int / int 进行整数除法。 尝试在操作中将其中一个 int 转换为 double 。
If you are looking for 0 < a < 1 answer, int / int will not suffice. int / int does integer division. Try casting one of the int's to a double inside the operation.
如此标记的答案非常接近,但我认为值得补充的是,使用双精度和十进制之间存在差异。
我不会比维基百科更好地解释这些概念,所以我只会提供指针:
floating -点算术
十进制数据类型
在金融系统中,通常要求我们可以保证一定数量(以 10 为基数)的小数位精度。 如果输入/源数据以 10 为基数,但我们以 2 为基数执行算术,这通常是不可能的(因为数字的小数扩展所需的小数位数取决于基数;三分之一需要无穷多个小数)以 10 为基数表示为 0.333333...,但以 3 为基数表示只需要一位小数:0.1)。
浮点数的处理速度更快(就 CPU 时间而言;在编程方面它们同样简单),并且当您想要最小化舍入误差时(如在科学应用中),浮点数是首选。
The answer marked as such is very nearly there, but I think it is worth adding that there is a difference between using double and decimal.
I would not do a better job explaining the concepts than Wikipedia, so I will just provide the pointers:
floating-point arithmetic
decimal data type
In financial systems, it is often a requirement that we can guarantee a certain number of (base-10) decimal places accuracy. This is generally impossible if the input/source data is in base-10 but we perform the arithmetic in base-2 (because the number of decimal places required for the decimal expansion of a number depends on the base; one third takes infinitely many decimal places to express in base-10 as 0.333333..., but it takes only one decimal in base-3: 0.1).
Floating-point numbers are faster to work with (in terms of CPU time; programming-wise they are equally simple) and preferred whenever you want to minimize rounding error (as in scientific applications).