round

Full name
round
Library
Built-in
Syntax

round(<number>[, <digits>])

Description

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.

Parameters
  • number: Number to round.
  • digits: (Optional) number of decimal places to include in rounding. It can be any whole number, positive, zero, or negative.
Examples

In this first example, a value close to PI is approximated to 2 and 4 decimal places, respectively:

Round function. Example of use

If the number of digits is not specified, the function returns an integer:

Round function. Example of use

However, if 0 decimal places are specified, the function returns a real number:

Round function. Example of use

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:

Round function. Example of use

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:

Round function. Example of use

In the following examples, the multiples closest to 10 ** (- (- 1)) are 10 and 20, respectively:

Round function. Example of use
Submitted by admin on Sat, 01/19/2019 - 19:37