Round decimal in c # always to the highest number

1

I'm trying to round a decimal number, but what normally happens is that if the number is assuming 40.40 stays in 40 , and if it's 40.60 goes up to 41 my code is as follows:

decimal price = 13224.60m;
int aux = (int) Math.Round(Convert.ToDouble(price), 0, MidpointRounding.ToEven);

as well as this code if I execute it results in 13225 , but if I change the value of price to 13224.40 :

decimal price = 13224.40m;
int aux = (int) Math.Round(Convert.ToDouble(price), 0, MidpointRounding.ToEven);

results in 13224 , and what I want is that I always round it up, that is, in either case I round it to 13225 .

Is there any way to do that? and if there is, how would the implementation be?

    
asked by José Gregorio Calderón 25.03.2017 в 20:36
source

2 answers

4

You can use:

decimal price = Math.Ceiling(13224.40m);
    
answered by 25.03.2017 / 21:10
source
4

There are two methods in c # the first Math.Ceiling(decimal) that rounds the value entered to its integer greater immediately, and also Math.Floor(decimal) that rounds the value entered to its whole number lower Immediately, see examples:

Console.WriteLine("{0,7} {1,16} {2,14}", 
                     value, Math.Ceiling(value), Math.Floor(value));

//         Value          Ceiling          Floor
//       
//          7.03                8              7
//          7.64                8              7
//          0.12                1              0
//         -0.12                0             -1
//          -7.1               -7             -8
//          -7.6               -7             -8
    
answered by 25.03.2017 в 21:31