简体   繁体   English

何时使用Decimal.Round()而不是Math.Round()?

[英]When to use Decimal.Round() instead of Math.Round()?

I notice that Math.Round() is more flexible than Decimal.Round() , since it is able to handle everything that Decimal.Round() can, but also can do the same given double inputs. 我注意到Math.Round()Decimal.Round()更灵活,因为它能够处理Decimal.Round()可以处理的所有内容,但是也可以在给定double输入的情况下执行相同操作。 So is there any situation where using Decimal.Round() is better, or is using Math.Round() all the time a better idea? 那么有没有使用Decimal.Round()更好的情况,或者一直使用Math.Round()更好的想法?

Look at the .NET source code: http://referencesource.microsoft.com/#mscorlib/system/math.cs,4f39179a0098ab01 查看.NET源代码: http//referencesource.microsoft.com/#mscorlib/system/math.cs,4f39179a0098ab01

Decimal Math::Round(Decimal) is defined as a direct call to Decimal Decimal::Round(Decimal) so the two are semantic analogues. Decimal Math::Round(Decimal)定义为对Decimal Decimal::Round(Decimal)的直接调用,因此这两者是语义类似物。

I suppose by default I'd use Decimal::Round to avoid an extra function call, but the JIT should optimize that away anyway, so I'd probably use Math::Round to be consistent with other Math calls, if there were any, in the same code file. 我想默认情况下我会使用Decimal::Round来避免额外的函数调用,但JIT应该优化它,所以我可能会使用Math::Round与其他Math调用一致,如果有的话,在同一个代码文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM