Hi everyone today I was testing some stuff and find an issue.
See:
int a = 13;
int b = 10;
double c = a/b;
The result is 1
. or should it be like this ?
Hi everyone today I was testing some stuff and find an issue.
See:
int a = 13;
int b = 10;
double c = a/b;
The result is 1
. or should it be like this ?
Dividing an int
by int
performs integer division. If you want to perform decimal division, cast one of the operands to double
.
double c = (double)a / b;
// ---OR---
double c = a / (double)b;
Also, declaring c
as double
does not guarantee decimal division. It can be an implicitly typed variable too. As long as one or both of the operands of the /
operator is of type double
(or float
, decimal
, etc.) you will get a decimal result.
you have to specify that you want a double
precision, for example
int a = 13;
int b = 10;
double c = a/(double)b;