How do I round numbers to the nearest integer?

Asked

Viewed 30,199 times

14

I have numbers double as

double a = 0.4, b = 0.5;

How do I round it up?

3 answers

25


For a simple rounding use the method Math.Round which receives a double (or decimal) and rounded to the nearest integer:

Math.Round(0.4); // 0
Math.Round(0.6); // 1

However, this method has one however: if the number to be rounded is halfway between an integer and another (ex.: 0.5) it will always round to the number par:

Math.Round(1.5); // 2
Math.Round(2.5); // 2

To have greater control over the result, use the overloaded method (overloaded) which accepts the additional parameter MidpointRounding. It is an enumeration, which accepts the following values:

  • AwayFromZero (choose the integer more "far" from zero, that is: the largest, if the parameter is positive, the smallest if it is negative)
  • ToEven (chooses the integer pair, similar to standard behavior)

Note that there are cases where the above options are not enough: if you want the number to be rounded down to zero (i.e. the integer with the smallest absolute value), and its parameters may include negative numbers, more than one operation may be required. An example would be:

public double PontoMedioDirecaoZero(double d) {
    double absInt = Math.Floor(Math.Abs(d)); // Piso do valor absoluto
    double absDec = Math.Round(Math.Abs(d) - absInt); // Arredondamento do valor absoluto
    return Math.Sign(d) * (absInt + absDec); // "Monta" o resultado final
}
PontoMedioDirecaoZero(-1.5); // -1

A similar strategy may be needed if rounding off at all times (and not only at the midpoint) down (Math.Floor) or upward (Math.Ceiling), taking into account the absolute value:

Math.Floor(0.6); // 0
Math.Floor(-0.6); // -1
Math.Sign(-0.6) * Math.Floor(Math.Abs(-0.6)); // 0
  • 2

    The correct is Math.Ceiling(0.5); for rounding up

  • @Guilhermealmeida I corrected the reply. Thank you!

3

To static class Math contains methods for rounding numbers:

Use Math.Ceil to round up

Math.Ceil(0.5); // 1

Use Math.Round to round, in this method you can enter a second parameter to determine whether 0.5 round to 1 or 0.

Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
Math.Round(0.5);                                // 0

Use Math.Floor round down

Math.Floor(0.5); // 0

Note that the return methods are double, if you try to convert a very large number to int you will receive a OverflowException.

  • 3

    The correct is Math.Ceiling(0.5); for rounding up

2

I had this problem and none of the suggestions I researched gave results.

Needed to round up 43,2549 same as the Excel that gives the result 43,26. C# would call me back 43,25 at all times.

then I did the following:

   decimal resultado = Math.Round(43.2549, 3, MidpointRounding.AwayFromZero);

Next:

resultado = Math.Round(resultado, 2, MidpointRounding.AwayFromZero);

It didn’t look elegant, but it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.