Operator Scope of Action:
Operator First Added:
Operator Last Altered:
Operator Has Newer Dot-Operator Variant:
Function [other Function type actions]
Item [operators of similar scope]
Mathematical [other Mathematical operators]
As at baseline
round() rounds the value of its sourceNum argument to the nearest integer. Note that from v9.5.2, there is now a dot-operator version (with the same behaviour) Number.round().
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"
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);
whereas if you used round(), as in:
$MyNumber = 100*round($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);