Problem to show the number's decimal?

ken76

Member
Joined
Nov 15, 2018
Messages
23
Programming Experience
5-10
I have problem with the code below this text. It doesn't show any decimals, only the number 0. Can someone help me to correct this code, so it shows also the decimals. In this case it enough with 2 decimals.

double answerCount;
answerCount = 5 / 20;
answerCount = Math.Round((Double)answerCount, 2);
 
The reason is because you are doing integer division, not floating point division, here:
C#:
answerCount = 5 / 20;

5 whole objects cannot be divided into 20 whole objects so the result is 0. 0 to two decimal places is 0.00.
 
As suggested, an int divided by an int always results in an int. If you want a double result then at least one of the operands must be a double. If you read the documentation for the division operators for the two types you'll see that.
 
Back
Top Bottom