What is the difference between Math.Round and decimal.Round functions in C# ?
The Math.round() function returns the value of a number rounded to the nearest integer.
If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down. Example: 33 rounded to the nearest ten is 30.
In C#, Math. Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method has another overload with which, you can specify the number of digits beyond the decimal point in the returned value.
You can multiply the original number by a power of 10 so that the desired place to round is in the unit's place, apply the "add half and round" method you already have, then divide the same power of 10, so the resulting number is now back to the original scale.
There is no difference.
Math.Round(decimal)
source code:
public static Decimal Round(Decimal d) {
return Decimal.Round(d,0);
}
Reference Source .NET Framework
EDIT:
To clarify, source code for decimal.cs
class:
public static Decimal Round(Decimal d) {
return Round(d, 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With