How to Round to the nearest whole number in C#

238,981

Solution 1

See the official documentation for more. For example:

Basically you give the Math.Round method three parameters.

  1. The value you want to round.
  2. The number of decimals you want to keep after the value.
  3. An optional parameter you can invoke to use AwayFromZero rounding. (ignored unless rounding is ambiguous, e.g. 1.5)

Sample code:

var roundedA = Math.Round(1.1, 0); // Output: 1
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero); // Output: 2
var roundedC = Math.Round(1.9, 0); // Output: 2
var roundedD = Math.Round(2.5, 0); // Output: 2
var roundedE = Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // Output: 3
var roundedF = Math.Round(3.49, 0, MidpointRounding.AwayFromZero); // Output: 3

Live Demo

You need MidpointRounding.AwayFromZero if you want a .5 value to be rounded up. Unfortunately this isn't the default behavior for Math.Round(). If using MidpointRounding.ToEven (the default) the value is rounded to the nearest even number (1.5 is rounded to 2, but 2.5 is also rounded to 2).

Solution 2

Math.Ceiling

always rounds up (towards the ceiling)

Math.Floor

always rounds down (towards to floor)

what you are after is simply

Math.Round

which rounds as per this post

Solution 3

You need Math.Round, not Math.Ceiling. Ceiling always "rounds" up, while Round rounds up or down depending on the value after the decimal point.

Solution 4

there's this manual, and kinda cute way too:

double d1 = 1.1;
double d2 = 1.5;
double d3 = 1.9;

int i1 = (int)(d1 + 0.5);
int i2 = (int)(d2 + 0.5);
int i3 = (int)(d3 + 0.5);

simply add 0.5 to any number, and cast it to int (or floor it) and it will be mathematically correctly rounded :D

Solution 5

You can use Math.Round as others have suggested (recommended), or you could add 0.5 and cast to an int (which will drop the decimal part).

double value = 1.1;
int roundedValue = (int)(value + 0.5); // equals 1

double value2 = 1.5;
int roundedValue2 = (int)(value2 + 0.5); // equals 2
Share:
238,981

Related videos on Youtube

SOF User
Author by

SOF User

Updated on March 28, 2020

Comments

  • SOF User
    SOF User about 4 years

    How can I round values to nearest integer?

    For example:

    1.1 => 1
    1.5 => 2
    1.9 => 2
    

    "Math.Ceiling()" is not helping me. Any ideas?

  • Only Bolivian Here
    Only Bolivian Here over 12 years
    What about 1.5 as the value? You need more parameters.
  • davogotland
    davogotland over 12 years
    on the other hand, using away from zero also means that -1.5 will round to -2.
  • ver
    ver over 11 years
    It still looks suspicious. Firstly, the question asks about rounding up and secondly, when I tried it just now, the default implementation of Math.Round(1.5) rounds to 2. So this may not be what he wanted.
  • ver
    ver over 11 years
    also, your example mixes decimal point with decimal comma. Which one do you normally use (in Sweden, I guess)? :)
  • davogotland
    davogotland over 11 years
    oops... oh yeah, sorry. in programming the decimal point of course, but in formal text we use the decimal comma. and yes, sweden ^^ about the question, and the "rounding up" part: i think that's just some language mistake. in the examples given by op, some decimal numbers round down.
  • davogotland
    davogotland over 11 years
    @ver i don't round down with Math.Round, i do it with a cast. that's why this way is manual and kinda cute ;)
  • Yakir Manor
    Yakir Manor over 10 years
    use Math.Ceiling, its not a good practice to use Math.Round for frictions, read: stackoverflow.com/questions/9221205/…,
  • David Sykes
    David Sykes over 10 years
    I am finding that Math.Round(1.5, 0) returns 2
  • sam
    sam about 7 years
    @davogotland is their anyway to round 137.5 to 140 not to 138 ? I mean rounding to nearest tenth ?
  • sam
    sam about 7 years
    is their anyway to round 137.5 to 140 not to 138 ? I mean rounding to nearest tenth ?
  • davogotland
    davogotland about 7 years
    @sam perhaps divide by 10, then round with Math.Ceiling, and finally multiple by 10?
  • monkey0506
    monkey0506 almost 7 years
    @DavidSykes I submitted an edit with some clarifications, because the behavior described was totally incorrect. 1.5 with MidpointRounding.ToEven (the default) will round to 2 and not 1 as was described. As MSDN states, ToEven rounds a midpoint like 1.5 to the nearest even number, 2.
  • Nathan Prather
    Nathan Prather about 4 years
    I'm using this your method because I also need a string and .ToString("n0") takes care of rounding for me: 1.5m.ToString("n0") // returns "2"