round(<number>[, <digits>])
The round function rounds a number to a precision given by the number of decimal places specified as an argument. If the number of decimal places is not specified, 0 is taken as the default value.
The returned value is rounded to the nearest multiple of 10 ^ (- number of decimal places). If two multiples are equally close, the even value is taken.
If the number of digits is not specified for the decimal part, the function returns an integer. In any other case (including the case where zero digits are explicitly specified) the function returns a number of the same type that is being rounded.
The behavior of this function rounding real numbers may not be as expected. Thus round (2.675, 2) returns 2.67 instead of the logical 2.68. This is not a bug, but the result that most decimal fractions cannot be represented exactly as a real number. More information.
- number: Number to round.
- digits: (Optional) number of decimal places to include in rounding. It can be any whole number, positive, zero, or negative.
In this first example, a value close to PI is approximated to 2 and 4 decimal places, respectively:
If the number of digits is not specified, the function returns an integer:
However, if 0 decimal places are specified, the function returns a real number:
In the case that there are two values that are equally close (0.0 and 1.0, in the first example shown below, and -1.0 and 0.0 in the second), the value closest to the even solution is returned:
If a number is rounded to -1 decimal places, the function returns the value closest to a multiple of 10, zero in the case that the number is less than or equal to 5:
In the following examples, the multiples closest to 10 ** (- (- 1)) are 10 and 20, respectively: