ceil(x)

x = Any number.

 

This function rounds up (as in 'up to the ceiling') the number to the nearest whole number, so wholenumber in Example #1 would return 11. This function is particularly useful when checking for random numbers, because random(x) can return a real number (5.65, 10.3,etc), you can use ceil to round the number to a whole number.

 

Example #1:

wholenumber = ceil(10.4);

 

Please note that the code ceil(random(3)) can in rare cases return 0. This has been discussed so many times in the Q and A forum. Returning a 0 happens very, very rarely but it is a real possibility, therefore the code must include that possibility:

 

Example #2:

selection = ceil(random(3));

switch (selection)

{

case 0:

// the very, very rare situation happened

// 0 was selected because if random(3) returned 0, there is nothing to round up

// here should be NO code and NO break statement

// thus case 0 will give the same result as case 1

case 1:

// 1 was selected because if it was 0.x, it would be rounded up

break;

case 2:

// 2 was selected because if it was 1.x, it would be rounded up

break;

case 3:

// 3 was selected because if it was 2.x, it would be rounded up

}

 

(See the switch statement for further details on switch).

 

Thanks to Weird Dragon for corrections -  Revision #3