C#

decimal a = 1.994444M; Math.Round(a, 2); //returns 1.99

from http://stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c[^]

See more:

How can i format the decimal variable??

Suppose,

I declare decimal type variable i = 0;

and i is 6.1056.

Actually I want to get the value is 6.11

how will i do above problem????

Please help me....

Suppose,

I declare decimal type variable i = 0;

and i is 6.1056.

Actually I want to get the value is 6.11

how will i do above problem????

Please help me....

Have a look at Math.Round [^]

from http://stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c[^]

C#

decimal a = 1.994444M; Math.Round(a, 2); //returns 1.99

from http://stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c[^]

Permalink

Share this answer

Comments

Mohamed Mitwalli
23-May-12 8:38am

5+

Nilesh Patil Kolhapur
23-May-12 8:42am

my also 5+

VJ Reddy
23-May-12 9:45am

Good answer. 5!

Try this

C#

i = Math.Round(6.1056,2);

Permalink

Share this answer

Comments

VJ Reddy
23-May-12 9:45am

My 5!

As already given in Solution 1 and 2, the

Math.Round Method (Decimal, Int32, MidpointRounding)[^]

can be used as follows:

By default the

If the

then the above values will be rounded off to

`Round`

method of `Math `

class explained hereMath.Round Method (Decimal, Int32, MidpointRounding)[^]

can be used as follows:

C#

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 belowC#

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.
Permalink

Share this answer

Comments

Nelek
23-May-12 16:35pm

Nice explanation

VJ Reddy
23-May-12 20:46pm

Thank you, Nelek :)

Mohamed Mitwalli
23-May-12 17:15pm

5+

VJ Reddy
23-May-12 20:46pm

Thank you, Mohamed :)

Espen Harlinn
23-May-12 18:21pm

Informative :-D

VJ Reddy
23-May-12 20:46pm

Thank you, Espen :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900