I have two integer variables which I need to divide in order to work the percentage out for something. I have (variablea / variableb) * 100. The problem is that the (variablea / variableb) will be between 0 and 1 so it gets rounded to 0 because it is an int. How can I get round this so the answer isn’t always 0?
Share
Try
(variablea * 100) / variableb.This will truncate the result. If you’re rather round to the nearest whole percent, you could do
(variablea * 100 + variableb/2) / variableb.Finally, instead of
100you could use a constant such as1000or10000if you’d like to get more decimal places (just remember to format the number correctly when printing, i.e. scale it by10or100).