I'm trying to round a decimal number, but what normally happens is that if the number is assuming 40.40
it stays in 40
, and if it is it 40.60
goes up, 41
my code is as follows:
decimal price = 13224.60m;
int aux = (int) Math.Round(Convert.ToDouble(price), 0, MidpointRounding.ToEven);
like this the code if I execute it gives me the result 13225
, but if I change the value of price
a 13224.40
:
decimal price = 13224.40m;
int aux = (int) Math.Round(Convert.ToDouble(price), 0, MidpointRounding.ToEven);
gives me the result 13224
, and what I want is that I always round it up, that is, that in any of the two cases I round it to 13225
.
is there any way to do that ? And if there is, how would the implementation be?
You can use:
There are two methods in C#, the first one that rounds the input value to its immediate larger
Math.Ceiling(decimal)
integer , and also that rounds the input value to its immediate smaller integer , see these examples:Math.Floor(decimal)