113

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?

amirfg
  • 272
  • 2
  • 6
  • 21
SOF User
  • 7,590
  • 22
  • 75
  • 121

16 Answers16

236

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).

monkey0506
  • 2,489
  • 1
  • 21
  • 27
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
  • 15
    on the other hand, using `away from zero` also means that `-1.5` will round to `-2`. – davogotland Jan 13 '12 at 01:35
  • 2
    use Math.Ceiling, its not a good practice to use Math.Round for frictions, read: http://stackoverflow.com/questions/9221205/why-does-system-midpointrounding-awayfromzero-not-round-up-in-this-instance, – Yakir Manor Sep 16 '13 at 11:27
  • 3
    I am finding that Math.Round(1.5, 0) returns 2 – David Sykes Jan 20 '14 at 13:45
  • @davogotland is their anyway to round 137.5 to 140 not to 138 ? I mean rounding to nearest tenth ? – sam Feb 27 '17 at 11:50
  • 3
    @sam perhaps divide by 10, then round with Math.Ceiling, and finally multiple by 10? – davogotland Mar 01 '17 at 09:54
  • @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. – monkey0506 Jul 11 '17 at 06:13
76
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

Community
  • 1
  • 1
devrooms
  • 3,119
  • 19
  • 24
  • is their anyway to round 137.5 to 140 not to 138 ? I mean rounding to nearest tenth ? – sam Feb 27 '17 at 11:53
8

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.

Dozer789
  • 1,980
  • 2
  • 23
  • 43
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
7

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

davogotland
  • 2,718
  • 1
  • 15
  • 19
  • 1
    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 Nov 20 '12 at 07:46
  • also, your example mixes decimal point with decimal comma. Which one do you normally use (in Sweden, I guess)? :) – ver Nov 20 '12 at 07:48
  • 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 Nov 20 '12 at 18:09
  • @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 ;) – davogotland Nov 20 '12 at 18:11
5

Just a reminder. Beware for double.

Math.Round(0.3 / 0.2 ) result in 1, because in double 0.3 / 0.2 = 1.49999999
Math.Round( 1.5 ) = 2
Kosmas
  • 353
  • 4
  • 11
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
Marlon
  • 19,924
  • 12
  • 70
  • 101
3

You have the Math.Round function that does exactly what you want.

Math.Round(1.1) results with 1
Math.Round(1.8) will result with 2.... and so one.
user496607
  • 442
  • 1
  • 10
  • 21
2

this will round up to the nearest 5 or not change if it already is divisible by 5

public static double R(double x)
{
    // markup to nearest 5
    return (((int)(x / 5)) * 5) + ((x % 5) > 0 ? 5 : 0);
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
rick
  • 21
  • 1
2

I was looking for this, but my example was to take a number, such as 4.2769 and drop it in a span as just 4.3. Not exactly the same, but if this helps:

Model.Statistics.AverageReview   <= it's just a double from the model

Then:

@Model.Statistics.AverageReview.ToString("n1")   <=gives me 4.3
@Model.Statistics.AverageReview.ToString("n2")   <=gives me 4.28

etc...

mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • 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" – Nathan Prather Apr 15 '20 at 13:35
2

Using Math.Round(number) rounds to the nearest whole number.

codejockie
  • 9,020
  • 4
  • 40
  • 46
1
var roundedVal = Math.Round(2.5, 0);

It will give result:

var roundedVal = 3
Liam
  • 27,717
  • 28
  • 128
  • 190
Ankita_Shrivastava
  • 1,225
  • 11
  • 9
1

Use Math.Round:

double roundedValue = Math.Round(value, 0)
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

If your working with integers rather than floating point numbers, here is the way.

#define ROUNDED_FRACTION(numr,denr) ((numr/denr)+(((numr%denr)<(denr/2))?0:1))

Here both "numr" and "denr" are unsigned integers.

Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
0

Write your own round method. Something like,

function round(x) rx = Math.ceil(x) if (rx - x <= .000001) return int(rx) else return int(x) end

Sandeep Anand
  • 99
  • 2
  • 13
0

this is what I was actually looking for

float myRound(float numberIn, int numDig) 
{ 
    // float numberIn: number to round
    // int numDig: number of digits after the point
    // myRound(123.456,2) => 123.45
    // myRound(123.456,1) => 123.4
    // myRound(123.456,-1) => 120
    float tenPow = Mathf.Pow(10, numDig);
    return ((int)tenPow*numberIn)/tenPow;
}
Adi Shumely
  • 367
  • 3
  • 5
-1
decimal RoundTotal = Total - (int)Total;
if ((double)RoundTotal <= .50)
   Total = (int)Total;
else
   Total = (int)Total + 1;
lblTotal.Text = Total.ToString();
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Salma
  • 1