Rounding and random numbers
TapHome script functions for rounding and random numbers — ROUND, CEIL and FLOOR with significance, RAND and RANDINT.
Functions that round a number or produce a random one. All of them return a double. Other arithmetic functions are on Mathematical functions.
ROUND
ROUND rounds a number to the nearest whole number; a half is rounded away from zero (2.5 to 3, -2.5 to -3).
| |
Remarks — ROUND has no argument for decimal places. To round to one decimal place, multiply by 10, round and divide by 10; use 100 for two places.
Example
ROUND(2.49);
# Result: 2
ROUND(2.5);
# Result: 3
ROUND(-2.5);
# Result: -3
ROUND(21.46 * 10) / 10;
# Result: 21.5CEIL
CEIL rounds a number up to the next whole number, or to the next multiple of significance when it is given.
| |
Parameters
| Name | Type | Description |
|---|---|---|
number | number | Value to round. |
significance | number, optional | Multiple to round to, for example 0.5 or 5. Default 1. It must not be 0, and it must not be negative when number is positive. |
Remarks — With a positive significance the result moves towards plus infinity, so a negative number moves towards zero (CEIL(-22.25, 1) is -22). With a negative significance a negative number moves away from zero (CEIL(-22.25, -1) is -23).
Example
| Call | Result |
|---|---|
CEIL(22.25) | 23 |
CEIL(22.25, 0.5) | 22.5 |
CEIL(22.25, 5) | 25 |
CEIL(-22.25) | -22 |
CEIL(-22.25, -1) | -23 |
CEIL(-22.25, -5) | -25 |
Errors — Significance cannot be 0.; Significance cannot be negative value if a number is a positive value.
FLOOR
FLOOR rounds a number down to the previous whole number, or to the previous multiple of significance when it is given.
| |
Parameters and Errors are the same as for CEIL.
Remarks — With a positive significance the result moves towards minus infinity, so a negative number moves away from zero (FLOOR(-22.25, 1) is -23). With a negative significance a negative number moves towards zero (FLOOR(-22.25, -1) is -22).
Example
| Call | Result |
|---|---|
FLOOR(22.25) | 22 |
FLOOR(22.25, 0.5) | 22 |
FLOOR(17, 5) | 15 |
FLOOR(-22.25) | -23 |
FLOOR(-22.25, -1) | -22 |
FLOOR(-22.25, -5) | -20 |
A whole-number division, for example days from seconds:
VAR uptime := 200000;
FLOOR(uptime / 86400);
# Result: 2RAND
RAND returns a random number from 0 up to, but not including, 1; every call gives a new value.
| |
Example
RAND() * 100;
# Result: a random number from 0 to just below 100RANDINT
RANDINT returns a random whole number from min up to, but not including, max.
| |
Returns — double holding a whole number. Use whole numbers for both limits: with a decimal min the result can be lower than min.
Example
RANDINT(1, 7);
# Result: 1, 2, 3, 4, 5 or 6