int countBouncy=5;
int count=999899;
double percent = (double)(countBouncy / count) * 100.0;
The result of that phrase is unexpected, I get zero.
Why won’t it work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are doing an integer division on
(countBouncy / count). Change your code toThat way,
countBouncyis converted todoubleexplicitly andcountis converted todoubleimplicitly by the c# compiler to make it compatible to the (nowdouble)countBouncy.Otherwise
(countBouncy / count)is calculated as(5 / 999899) --> 0since both are integers.How does integer division work? Let’s take an example:
Integer division drops the decimal part that a real division would yield. The result is truncated towards zero. You can get the remainder of this division by using the modulo operator
and perform the backward calculation like this
You can enter this in the immediate window of Visual Studio to test it: