Operator Type:
Operator Scope of Action:
Operator Purpose:
Operator First Added:
Operator Altered:
Function [other Function type actions]
Item [operators of similar scope]
Mathematical [other Mathematical operators]
Baseline
round(argument)
round() rounds the value of its argument to the nearest integer.
The Number.format, Number.precision and format() functions all round numbers in this manner when shortening numbers during formatting. A partial integer over .50 always round up, everything else rounds down. Thus:
$MyNumber = round(4.0);
gives 4
$MyNumber = round(4.2);
gives 4
$MyNumber = round(4.5);
gives 5
$MyNumber = round(4.7);
gives 5
The round function with work on string literal or String attribute values that are just numbers:
$MyNumber = round("4.2");
gives 4
$MyNumber = round("4.7");
gives 5
$MyString = round(4.2);
gives "4"
$MyString = round(4.7);
gives "5"
$MyString = round("4.2");
gives "4"
$MyString = round("4.7");
gives "5"
There are no functions to always force a round down (floor) or up (ceiling), but these can be simulated with a bit of action code.
For a practical example, assume you would like to round you calculation (upward) to the nearest 100, so if the calculation output is 167 it should be 200, for a result of 540 a result of 600, and so on. This can be done like so:
$MyNumber = 100*ceil($MyNumber/100);
as if you used round(), as in:
$MyNumber = 100*ceil($MyNumber/100);
the result would vary depending on whether $MyNumber started above or below the nearest 50. An opposite of the first example, i.e. where everything rounds downwards to the nearest 100 can be done using floor():
$MyNumber = 100*floor($MyNumber/100);