How to divide integers and get value with decimal part?

Asked

Viewed 1,793 times

8

I am trying to divide the following values into :

Double media = 0.0;
int soma = 7;
int cont = 2;
media = soma / cont;

Is returning 3.

3 answers

13


This is about typing. You’re splitting 2 integers, so you get an integer, if you want a result that’s not an integer, you need to split numbers that aren’t integers, you can make a cast, it’s safe to do something that increases accuracy:

using static System.Console;

public class Program {
    public static void Main() {
        int soma = 7;
        int cont = 2;
        double media = (double)soma / (double)cont;
        WriteLine(media);
    }
}

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

You don’t need to convert both operands if one of them is double, the result will already be double to ensure there is no loss of accuracy.

  • continues to give me whole

  • I’m showing you I can’t.

  • so I always need to cast using (double) ?

  • If that’s what you want, yes. But you may not want that, you first need to know what you want.

  • @Amadeuantunes, a calculation with two integers will always return an integer type value. It seems strange at first, but for the compiler it makes no sense to change the typing, regardless of the calculation being done. Computer does not err and does exactly what is ordered. Another problem is to use doublefor the calculation. double is a base floating binary, while decimal is a base floating decimal, which is what we use. You may get unexpected results if you use double without really knowing its characteristics.

  • @Thiagolunardi so I should use decimal?

  • 1

    @Amadeuantunes depends on what you want to do, depends on the accuracy you need, the double is suitable for many situations.

  • Decimal is recommended for money. As fellow @bigown said, it will depend on what you will do.

  • @Amadeuantunes if you want to store decimal values, such as monetary, dimensions, etc, yes, use Decimal

  • 1

    @mustache need not put (double)soma / (double)cont;, just leave it (double)soma / cont;, because this conversion is already redundant.

  • 1

    @Matheusmiranda I put both and wrote that you do not need in the answer, then the person chooses as you wish.

Show 6 more comments

4

There’s no point in you using the type int that will always return integer number.

You can use type decimal or double depending on what you do:

decimal media = 0 , soma = 7 , cont = 2;
media = soma / cont; //retorna 3.5

Follow another way simpler and easier:

double soma = 7;
double cont = 2;
var media = soma / cont; //retorna 3.5

Or you can use Convert.ToDouble() or Convert.ToDecimal().

Double media = 0;
int soma = 7;
int cont = 2;
media = Convert.ToDouble(soma) / cont; // retorna 3.5

1

public class Program {
        public static void Main() {
            double soma = 7;
            double cont = 2;
            double media = soma / cont;
            WriteLine(media);
        }
    }

Browser other questions tagged

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