Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Require function to round

Posted on 2013-06-03
5
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 25

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

704 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