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

5
我注意到 Math.Round()Decimal.Round() 更加灵活,因为它能够处理所有 Decimal.Round() 能够处理的情况,而且还能够处理 double 类型的输入。那么,是否存在一些使用 Decimal.Round() 更好的情况,或者说始终使用 Math.Round() 更好呢?
1个回答

4

查看 .NET 的源代码:http://referencesource.microsoft.com/#mscorlib/system/math.cs,4f39179a0098ab01

Decimal Math::Round(Decimal) 定义为直接调用 Decimal Decimal::Round(Decimal),因此两者是语义上的类比。

我认为默认情况下应使用 Decimal::Round 来避免额外的函数调用,但 JIT 应该会对其进行优化,因此如果同一代码文件中有其他 Math 调用,我可能会使用 Math::Round 以保持一致性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接