• Status: Solved
• Priority: Medium
• Security: Public
• Views: 340

# C# rounding function

Please refresh my memory on rounding in C#.    For example, if we have this number scenario:

93.5433

but we want this to be rounded UP every time to this:

93.5500

How to do that in C# ?

Thanks.

- - -  - - - -

Another example:

we have this:

34.7985

to be rounded UP to:

34.8000
0
lblinc
3 Solutions

Commented:
well .. I guess the following mathematical algorithm should work just fine:

Math.Ceiling((x*100))/100
0

Commented:
Math.Round(x,2);

This will round to the second decimal place.
0

Commented:
but for the first example, that will get: 93.54 no?
0

Commented:
Or you could try Decimal.Round(x, 2);  This is same as GENTP.  Up to you which one you want to use.  Sometimes it's useful to use Decimal.Round because it's easier to tell what type of number you are rounding.

Cheers,

Aaron
http://aaronfeng.blogspot.com/
0

Commented:
Hi  lblinc,

See if this works:

double val = 34.7900;
if( val > (Math.Round(val,2)))
{
val += .01;
}

I checked it with your two example values and it rounded correctly.

Vaughn
0

Commented:
I forgot the rounding :

double val = 93.5433;
if( val > (Math.Round(val,2)))
{
val += .01;
}

Math.Round(val, 2)  //forgot this

Vaughn
0

Commented:
geez... I forgot the ';' at the end:

double val = 93.5433;
if( val > (Math.Round(val,2)))
{
val += .01;
}
MessageBox.Show(Math.Round(val, 2).ToString());

Vaughn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.