Choose where you’d like to start

round

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,

ParameterData typeDescription
<variable>DECIMALThe variable that will contain the rounded off value.
<numericalExpression>DECIMAL/NUMBERThe value to be rounded off to the given precision.
<roundingPrecision>NUMBER

The precision(number of rounding digits) to which the number is to be rounded.

When the value is positive:

  • A positive value will round off the number to the specified number of positions to the right of the decimal point.
  • If the value to the right of the rounding digit is greater than or equal to 5, the function will add 1 to the rounding digit, and all the digits after the rounding digit will be dropped.
  • If the value to the right of the rounding digit is lesser than or equal to 4, all digits to the right of the rounding digit will simply be dropped (without consideration).

When the value is negative:

  • A negative value will round off the number to the specified number of positions to the left of the decimal point.
  • A negative value which is equal to or larger than the number of digits before the decimal point, will return 0.
  • If the rounding digit is greater than or equal to 5, the function will add 1 to the digit on the left of the rounding digit, and all values following it (including the rounding digit) will become 0.
  • If the rounding digit is lesser than or equal to 4, all digits except the ones to the left of the rounding digit will become 0.
  • The rounded off number will be stripped off the digits after the decimal point.

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.

Related Links

Get Started Now

Execute