8
I am trying to divide the following values into c#:
Double media = 0.0;
int soma = 7;
int cont = 2;
media = soma / cont;
Is returning 3
.
8
I am trying to divide the following values into c#:
Double media = 0.0;
int soma = 7;
int cont = 2;
media = soma / cont;
Is returning 3
.
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.
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 c# .net operators typing mathematics
You are not signed in. Login or sign up in order to post.
continues to give me whole
– Amadeu Antunes
I’m showing you I can’t.
– Maniero
so I always need to cast using (double) ?
– Amadeu Antunes
If that’s what you want, yes. But you may not want that, you first need to know what you want.
– Maniero
@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
double
for the calculation.double
is a base floating binary, whiledecimal
is a base floating decimal, which is what we use. You may get unexpected results if you usedouble
without really knowing its characteristics.– Thiago Lunardi
@Thiagolunardi so I should use decimal?
– Amadeu Antunes
@Amadeuantunes depends on what you want to do, depends on the accuracy you need, the
double
is suitable for many situations.– Maniero
Decimal is recommended for money. As fellow @bigown said, it will depend on what you will do.
– Matheus Miranda
@Amadeuantunes if you want to store decimal values, such as monetary, dimensions, etc, yes, use Decimal
– Thiago Lunardi
@mustache need not put
(double)soma / (double)cont;
, just leave it(double)soma / cont;
, because this conversion is already redundant.– Matheus Miranda
@Matheusmiranda I put both and wrote that you do not need in the answer, then the person chooses as you wish.
– Maniero