I have two integers (amount of bytes of two files). One is always smaller if not the same than the other. I want to calculate the percentage that the smaller is of the bigger.
I'm using plain C. I've applied the math formula, but am getting always 0:
printf("%d\r", (current/total)*100);
Any ideas?
Try
printf("%g\r", (((double)current)/total)*100);
instead. Integer division always rounds towards zero. By converting one of the numbers to double
first, you will trigger floating point division. If you would like to use integer arithmetic, you could also use
printf("%d\r", (100*current)/total);
which will print the percentage rounded down to the next integer.
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