Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between decimal.Round and Math.Round

Tags:

c#

What is the difference between Math.Round and decimal.Round functions in C# ?

like image 239
Ehsan Hafeez Avatar asked Jul 18 '16 17:07

Ehsan Hafeez


People also ask

What does Math round do?

The Math.round() function returns the value of a number rounded to the nearest integer.

Does Math round round up or down?

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.

What is the use of Math round in C#?

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.

How do you round without using Math round?

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.


1 Answers

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);
}
like image 136
apocalypse Avatar answered Oct 13 '22 02:10

apocalypse