Rounding up

Asked

Viewed 201 times

6

I have these values, where contrato.Count = 63 and 63/50 = 1.26. I’d like you to round up 2, I’m doing it this way:

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

But it always brings me one.

  • 1

    But to round off to 2 the value should not be greater than 1.50?

  • So that’s why I need to check, because it always needs to be up.

  • 1.01 would also stay 2? 1.00 would stay 1 right?

  • That’s it. Because I need to generate a batch, if 1.01 means you still have something to generate, and so it needs to be up.

  • A the result of 2 integer numbers is always an integer number. Tries to convert the numbers to decimal and checks the return.

3 answers

9


The result of dividing two whole numbers will always be an integer (no .Net). So convert the values to decimal and use the Math.Ceiling you will have the value rounded up.

Math.Ceiling(Convert.ToDecimal(63)/ Convert.ToDecimal(50));
  • I converted to decimal, and it worked.

  • 4

    This conversion is unnecessary. In the case of literals just add suffix m so that the compiler interprets the decimal division: Math.Ceiling(63m / 50m);. In the case of variables and constants only cast the operation Math.Ceiling((Decimal) varA / varB). Code in the Fiddlel

  • This suffix I didn’t know, very cool and practice.

  • 1

    @Augustovasques +1: Great placement. Just one correction: the casting you used for example is not of the operation, it is of the operand varA, if it were the transaction the value would be the entire result of the "casted" operation as decimal.

6

To round up use static method Math.Ceiling who 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 in Repl.it.

6

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 so:

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

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

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.