round
Table of Contents
Overview
The round function takes a numerical expression and rounding precision as arguments and returns the rounded off value.
Return Type
Syntax
<variable> = <numericalExpression>.round(<roundingPrecision>);
where,
Parameter | Data type | Description |
<variable> | DECIMAL | The variable that will contain the rounded off value. |
<numericalExpression> | DECIMAL/NUMBER | The value to be rounded off to the given precision. |
<roundingPrecision> | NUMBER | When the value is positive:
When the value is negative:
|
Examples
number1 = -218.4.round(0); // .4 is dropped as precision is 0 and -218 gets assigned to number1 number2 = 218.5.round(0); // the value 219 gets assigned to number2 number3 = 218.49.round(1); // the value 218.5 gets assigned to number3 number4 = 218.44.round(1); // the value 218.4 gets assigned to number4 number5 = 214.2.round(-1); // the value 210 gets assigned to number5 number6 = 218.2.round(-1); // the value 220 gets assigned to number6 number7 = 218.2.round(-2); // the value 200 gets assigned to number7 number8 = 294.2.round(-2); // the value 300 gets assigned to number8 number9 = 218.222.round(-3); // the value 0 gets assigned to number9 number10 = -150.55.round(-2); // the value -200 gets assigned to number10 number11 = -192.66.round(-1); // the value -190 gets assigned to number11 number12 = 1.4+3.6.round(0); // the value 5.4 gets assigned to number12 number13 = (1.4+3.6).round(0); // the value 5 gets assigned to number13
Resolving rounding expressions and their precedence:
- 1.3.round(0)+2.7.round(0)->1+3->4 (rounding first to nearest whole number then resolving the expression).
- 1.3.+2.7.round(0)->1.3+3->4.3 (rounding first to nearest whole number then resolving the expression).
- (1.3+2.9).round(0)->4.2round(0)->4 (rounding first to nearest whole number then resolving the expression).
- All the forms above are valid. Correctness depends on the case that is being dealt with.