将 C# 数字类型从一种转换为另一种的学术路线是什么?
根据我的测试,将小数或双精度数转换为 int 或 long 只会保留数字的整数部分并丢弃小数部分。相反,您可以拥有一个 int 变量并将其直接分配给一个decimal变量而不进行强制转换,它将执行隐式转换。
它有效,但仅仅因为它有效并不总是意味着它是最好的方法。这种类型的转换是否有任何性能影响,或者最好使用 Convert.ToWhatever() 吗?
Based on my tests, casting a decimal or double to an int or long simply keeps the whole number portion of the number and discards the decimal portion. Reversely, you can have an int variable and assign it directly to a decimal variable without casting and it will perform an implicit conversion.
It works, but just because it works doesn't always mean it's the best way. Are there any performance implications over this type of conversion or is it best to use Convert.ToWhatever()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Convert.ToWhatever() 的性能可能会较差。真正的问题是,无论您可能对性能产生什么影响,都将非常小,以至于它只是一种边缘情况。如果数字转换是一个性能问题,那么可能有另一种解决方案会更有帮助,并且可以更好地利用您的时间/金钱。
Convert.ToWhatever() is likely going to be less performant. The real point is, that whatever performance implications you might have are going to be so miniscule that it's an edge case. If number conversions are a performance problem, there's likely another solution that's going to be more helpful and a better use of your time / money.
您需要将十进制转换为 int 或将 double 转换为 int,因为转换会导致精度损失,因此您应该明确说明这一点,而不是让您无意中执行有损操作。
反之,双精度或小数可以完全表示从 int 分配的任何值,因此可以代表您隐式完成转换。
唯一的问题是,演员是否正在执行您需要的转换,即。只是简单地截断小数部分,或者您是否需要特定的舍入等。
在性能方面,将 double 转换为 int 只需使用
conv.i4
操作码,其中使用类似 Convert.ToInt32(double ) 导致函数调用来执行转换。对于decimal到int的转换将导致对运算符重载的调用,本质上是函数调用,当然使用Convert.ToInt32(decimal)又是函数调用。
在十进制的情况下,本质上没有区别,但 Convert.ToInt32(double) 甚至不会被内联,因此它的成本要高得多,但从更大的角度来看,成本是否显着,通常不是。
You require the cast for decimal to int or double to int because the conversion results in a loss of precision, therefore rather than allowing you to inadvertently perform a lossy operation you are expected to be explicit about it.
Going the other way around, a double or decimal can fully represent any value assigned from an int and therefore the conversion can be done implicitly on your behalf.
The only question is, is the cast doing the kind of conversion you require, ie. is simply truncating the fractional portion sufficient or do you require specific rounding etc.
In terms of performance, the cast of a double to int simply uses the
conv.i4
opcode where using something like Convert.ToInt32(double) results in a function call to perform the conversion.For decimal to int the cast will result in a call to an operator overload, essentially a function call, and of course using Convert.ToInt32(decimal) is again a function call.
In the case of decimal there is essentially no difference, but Convert.ToInt32(double) will not even be inlined so it is considerably more expensive, but is the cost significant in the bigger picture, typically not.
为什么不简单地使用 Math.Round() 将十进制或双精度数转换为整数?
请注意,Math.Round() 仍然返回小数/双精度,但现在已正确舍入,因此您可以将其转换为 int。
Why don't you simply use
Math.Round()
for converting decimal or double to int?Note that
Math.Round()
still returns decimal / double but now properly rounded, so you can cast it toint
.