Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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. So is there any situation where using Decimal.Round() is better, or is using Math.Round() all the time a better idea?

like image 923
ananda Avatar asked Sep 28 '22 14:09

ananda


1 Answers

Look at the .NET source code: 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.

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.

like image 67
Dai Avatar answered Oct 07 '22 18:10

Dai