As already given in Solution 1 and 2, the
Round
method of
Math
class explained here
Math.Round Method (Decimal, Int32, MidpointRounding)[
^]
can be used as follows:
decimal number = 6.1056M;
Console.WriteLine (Math.Round(number,2));
By default the
Round
method uses
MidpointRounding.ToEven
enumeration value explained here
http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx[
^], which means that if the value to be rounded is
6.105
it will be rounded off to
6.10
and if it is
6.115
then it will be rounded off to
6.12
as
5
is the
mid point
.
If the
MidpointRounding.AwayFromZero
is used as shown below
decimal number = 6.1056M;
Console.WriteLine (Math.Round(number,2, MidpointRounding.AwayFromZero));
then the above values will be rounded off to
6.11
and
6.12
respectively, i.e. always the number
away from zero
, in other words the
next higher number
will be taken.