Solved

Require function to round

Posted on 2013-06-03
5
258 Views
Last Modified: 2013-06-06
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
Comment
Question by:GRChandrashekar
5 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39218094
See very nice explanation on StackOverflow
0
 

Author Comment

by:GRChandrashekar
ID: 39218099
Where is the required function but ?
0
 
LVL 4

Expert Comment

by:MrC63
ID: 39218115
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 39218187
double d = 0.5;
double round = (d - Math.Floor(d)) >= .5 ? Math.Ceiling(d) : Math.Floor(d);

Open in new window

0
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 500 total points
ID: 39220929
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now