How to round up value C# to the nearest integer?
228,456
Solution 1
Use Math.Ceiling to round up
Math.Ceiling(0.5); // 1
Use Math.Round to just round
Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
And Math.Floor to round down
Math.Floor(0.5); // 0
Solution 2
Check out Math.Round. You can then cast the result to an int.
Solution 3
The .NET framework uses banker's rounding in Math.Round by default. You should use this overload:
Math.Round(0.5d, MidpointRounding.AwayFromZero) //1
Math.Round(0.4d, MidpointRounding.AwayFromZero) //0
Solution 4
Rounds a double-precision floating-point value to the nearest integral value.
Solution 5
Use a function in place of MidpointRounding.AwayFromZero:
myRound(1.11125,4)
Answer:- 1.1114
public static Double myRound(Double Value, int places = 1000)
{
Double myvalue = (Double)Value;
if (places == 1000)
{
if (myvalue - (int)myvalue == 0.5)
{
myvalue = myvalue + 0.1;
return (Double)Math.Round(myvalue);
}
return (Double)Math.Round(myvalue);
places = myvalue.ToString().Substring(myvalue.ToString().IndexOf(".") + 1).Length - 1;
} if ((myvalue * Math.Pow(10, places)) - (int)(myvalue * Math.Pow(10, places)) > 0.49)
{
myvalue = (myvalue * Math.Pow(10, places + 1)) + 1;
myvalue = (myvalue / Math.Pow(10, places + 1));
}
return (Double)Math.Round(myvalue, places);
}
Author by
william
I am working as a web developer in a software house which uses Sitecore and Ektron as engines and c#, linq and ajax to develop websites. I have also written some rdlc reports with SQL Server 2008 R2.
Updated on June 03, 2021Comments
-
william almost 2 yearsI want to round up double to int.
Eg,
double a=0.4, b=0.5;I want to change them both to integer.
so that
int aa=0, bb=1;aais fromaandbbis fromb.Any formula to do that?
-
william over 12 yearsit doesn't matter for me..i also found out that. -
Matthew Flaschen over 12 yearsThere's no error. 1/2 can be represented exactly as a double. The docs say, "If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned." 0 is the even number. This fudge factor will make numbers like 0.499999995 round wrong. -
jnielsen over 12 yearsYou need to do more work with Math.Round. It provides an enum (MidpointRounding) to specify the behaviour accurately. -
BrunoLM over 12 years@jnielsen: Thanks for pointing that and for the code example. I've corrected it. -
Will over 12 yearsCheers for the heads-up. I was trying to ensure the OP knew that Round didn't work as simply as others were suggesting. Thankfully the OP now knows to watch out for it, and I have a better understanding of Round myself. =) -
Admin over 11 yearsHowever: "The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. ..." -
Sean Reid over 7 yearsThis will fail if the current culture uses commas instead of full stops for the decimal point. -
yopez83 over 4 yearsThe simplest and effective way is Math.Ceiling -
phuclv about 4 yearswhy useMath.Floorwhen the OP wants to round to nearest? This won't work for negative values -
Kalu Singh Rao about 4 yearsThis example only for positive values. -
phuclv about 4 yearsyou didn't even mentioned that in your question. And the OP didn't say that he's only interested in positive integers. Anyway this is useless because there are already Math.Round -
Big Money almost 3 yearsPlease note thatMath.Ceilingwill return the same number if the number is already whole, it will NOT round up by 1 -
Nigrimmist about 1 yearLooks like some low-level coding, please, do not follow it :)