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

# Require function to round

Hi, I need function to round numbers in C#.

Though it is available, it rounds of 0.5 as 0. I need this to round off as 1 and not 0.

Means, 20.5 to round as 21 and 0.5 to round as 1 and 2.5 to round as 3 and so on...
But if it is 2.4 then it should round off as 2 as it does right now
0
GRChandrashekar
1 Solution

Commented:
See very nice explanation on StackOverflow
0

Author Commented:
Where is the required function but ?
0

Commented:
The quick and easy way to do this is to add .00001 value to the original value, and then use the Math.Round function.  If the value was originally 2.5, it will now be 2.50001.  By doing this, it won't matter if use Bankers method of rounding which rounds to the lower place when the value is at the midpoint.  The value will always be above the midpoint, but never high enough to impact anything other than the midpoint. This then then allows the Math.Round function to round upwards instead of down if or when the value is precisely halfway.
0

Full stack Software EngineerCommented:
``````double d = 0.5;
double round = (d - Math.Floor(d)) >= .5 ? Math.Ceiling(d) : Math.Floor(d);
``````
0

Software ConsultantCommented:

Math.Round(value, MidpointRounding.AwayFromZero);

Ref :
http://msdn.microsoft.com/en-us/library/ef48waz8.aspx
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.