Home » c# » How to Round to the nearest whole number in C#

How to Round to the nearest whole number in C#

Posted by: admin November 30, 2017 Leave a comment

Questions:

How can I round values like this:

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

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

Answers:

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

Questions:
Answers:
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

Questions:
Answers:

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 😀

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

Use Math.Round:

double roundedValue = Math.Round(value, 0)

Questions:
Answers:

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);
}

Questions:
Answers:

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…

Questions:
Answers:

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.

Questions:
Answers:
var roundedVal = Math.Round(2.5, 0);

It will give result:

var roundedVal = 3

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:
decimal RoundTotal = Total - (int)Total;
if ((double)RoundTotal <= .50)
   Total = (int)Total;
else
   Total = (int)Total + 1;
lblTotal.Text = Total.ToString();