I have a number (let's say, 34), and I want to find its next multiple of ten. I can do this by:
After a bit of research, I discovered that this is the code for that in Objective C:
int number = 34;
int roundedNumber = ceil((double)number/10)*10;
My question is: what is the (double)
for, and why does removing (double)
cause it to round down instead of up?
I understand from googling that changes the float format to "double precision" but, to be honest, this is way too complicated for me. Can anyone provide a simple explanation of what it is doing?
If you don't have the cast the following happens (if number is 34).
If you have the cast, the following happens:
The important thing to realise is Integer division always rounds towards 0.
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