Solved

Require function to round

Posted on 2013-06-03
5
277 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

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 65
Programmatically storing deadlock graphs with SQL Server 6 52
HttpPostedFile 1 30
defining NULL or 0 10 44
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

838 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