Sinking up

I have this values, where contrato.Count = 63 and 63/50 = 1.26. I would like it to yield to 2, I am doing this way:

 decimal arrendondamento = contrato.Count / 50;
 var valorArredondado = Math.Ceiling(arrendondamento);

But it always brings me 1.

Author: Maniero, 2019-04-29

3 answers

The result of splitting two integers will always be an integer (in .NET). So if you convert the values to decimal and use Math.Ceiling you will have the value rounded up.

Math.Ceiling(Convert.ToDecimal(63)/ Convert.ToDecimal(50));
 9
Author: Deividson Oliveira, 2019-04-29 13:04:24

To round up use the static method Math.Ceiling which accepts a Decimal or Double and returns the smallest integer value greater than or equal to the specified decimal number(rounds up).

Example:

using System;

class MainClass {

  public static void Main (string[] args) {

    double[] valores = {7.03, 7.64, 0.12, -0.12, -7.1, -7.6};

    Console.WriteLine("  Valor          Ceiling\n");

    foreach (var valor in valores)
       Console.WriteLine("{0,7} {1,16}", valor, Math.Ceiling(valor));
  }

}

         Valor          Ceiling          
          7.03                8              
          7.64                8              
          0.12                1              
         -0.12                0             
          -7.1               -7             
          -7.6               -7      

Code no Repl.it .

 6
Author: Augusto Vasques, 2019-04-29 13:24:23

The diagnosis of the other answers is correct, but the prognosis is not the best. The simplest way, and I would say more correct because it avoids conversion, to do this is like this:

decimal arrendondamento = contrato.Count / 50M;
var valorArredondado = Math.Ceiling(arrendondamento);

This way you have a number as decimal and the division occurs respecting this without having to convert anything that is inefficient and verbose. Note that the problem is typing in the split, the rounding method does not interfere with this, so I made a code that shows the result without applying it also:

using static System.Console;
using static System.Math;

public class Program {
    public static void Main() {
        var contador = 63;
        decimal arrendondamento = contador / 50M;
        WriteLine(arrendondamento);
        WriteLine(Ceiling(arrendondamento));
    }
}

See working on ideone. And no .NET Fiddle. Also I put on GitHub for future reference .

 6
Author: Maniero, 2019-05-21 12:22:12