What makes a "double" in the ceiling (double)?

I have a number (say 34) and I want to find its next multiple of ten. I can do it:

  • Dividing the number by 10
  • Rounding to integer
  • Multiplication by 10.

After a little research, I found that this is the code for Objective-C:

int number = 34; int roundedNumber = ceil((double)number/10)*10; 

My question is: what is (double) for, and why does deleting (double) make it round, not up?

I understand that from googling it changes the float format to "double precision", but to be honest, it's too complicated for me. Can someone give a simple explanation of what he is doing?

+4
source share
2 answers

If you do not have a throw, the following will happen (if the number is 34).

  • Using integer arithmetic, the number / 10 is rounded to the number / 10, i.e. 3.
  • ceil (3) = 3
  • 3 * 10 = 30

If you have a roll, the following will happen:

  • (double) number = 34.0
  • 34.0 / 10 = 3.4
  • ceil (3.4) = 4.0
  • 4.0 * 10 = 40

It is important to understand that integer division is always rounded to 0.

+5
source

It distinguishes number as double , so that instead of integer division, float division is performed. Compare 1/2 and 1.0/2 .

+4
source

All Articles