I want to calculate percentage and data here is completely dynamic.
int sample1 = 0;
int total = 0;
int finalValue = 0;
finalValue = ((sample1*100)/total)
Here finalValue
is printed exactly as I want when there is some data. But When the value from database is 0(zero) then this simple calculation gives an error. If you perform calculation in calculator it says "Result is undefined". So in such case i tried using if and else
condition.
if(sample1>0)
{
//execute code
}
else
{
//sample1 = 0;
}
This logic doesn't work here. So what would be easy and preferred way to perform percentage calculation.
You should check total first .
Division by zero is not allowed and that's why it gives an error
Note : != means not equal to