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

Math.Round

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);
}
Share:
228,456
william
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, 2021

Comments

  • william
    william almost 3 years

    I 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;
    

    aa is from a and bb is from b.

    Any formula to do that?

  • william
    william over 13 years
    it doesn't matter for me..i also found out that.
  • Matthew Flaschen
    Matthew Flaschen over 13 years
    There'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
    jnielsen over 13 years
    You need to do more work with Math.Round. It provides an enum (MidpointRounding) to specify the behaviour accurately.
  • BrunoLM
    BrunoLM over 13 years
    @jnielsen: Thanks for pointing that and for the code example. I've corrected it.
  • Will
    Will over 13 years
    Cheers 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
    Admin over 12 years
    However: "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
    Sean Reid over 8 years
    This will fail if the current culture uses commas instead of full stops for the decimal point.
  • yopez83
    yopez83 over 5 years
    The simplest and effective way is Math.Ceiling
  • phuclv
    phuclv about 5 years
    why use Math.Floor when the OP wants to round to nearest? This won't work for negative values
  • Kalu Singh Rao
    Kalu Singh Rao about 5 years
    This example only for positive values.
  • phuclv
    phuclv about 5 years
    you 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
    Big Money almost 4 years
    Please note that Math.Ceiling will return the same number if the number is already whole, it will NOT round up by 1
  • Nigrimmist
    Nigrimmist about 2 years
    Looks like some low-level coding, please, do not follow it :)