I`m trying to calculate a percent of successful jobs in total. Code:
int total = valid + invalid;
int percent = (valid / total) * 100;
if (percent == 0)
{
MessageBox.Show(Convert.ToString(total) + ":" + Convert.ToString(valid));
break;
}
If all job are successful, the percent is 100%. If one job is bad, percent is 0 and i get messagebox with: 9:8
8/9*100 = 88.888, not 0.
int percent = Convert.ToInt32((valid / total) * 100);
gives no result. Please, help me. I am Russian, so sorry for bad English.
I'm guessing both valid and invalid are defined as ints. In that case, the result of all calculations will be ints as well.
You either need to cast one of the values to floating point type in the operation or, and this would probably be easier, define one of the values as a floating point type to begin with:
double total = valid + invalid;
int percent = (valid / total) * 100;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With