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
GRChandrashekarAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Kalpesh ChhatralaConnect With a Mentor Software ConsultantCommented:
try like below. it will help you to Resolve your problem.

Math.Round(value, MidpointRounding.AwayFromZero);


Ref :
http://msdn.microsoft.com/en-us/library/ef48waz8.aspx
0
 
chaauCommented:
See very nice explanation on StackOverflow
0
 
GRChandrashekarAuthor Commented:
Where is the required function but ?
0
 
MrC63Commented:
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
 
Meir RivkinFull stack Software EngineerCommented:
double d = 0.5;
double round = (d - Math.Floor(d)) >= .5 ? Math.Ceiling(d) : Math.Floor(d);

Open in new window

0
All Courses

From novice to tech pro — start learning today.