48 of 151 menu

The round function

The round function rounds the floating-point number specified in the first parameter to the nearest integer according to the rules of mathematical rounding. The second optional parameter of the function specifies how many digits to leave in the fractional part.

Syntax

round(number, [signs in the fractional part])

Example

Let's round to whole numbers 3.458:

print(round(3.458))

Result of code execution:

3

Example

Let's round to whole numbers 3.6771:

print(round(3.6771))

Result of code execution:

4

Example

Now let's round the number to the second decimal place:

print(round(3.458, 2))

Result of code execution:

3.68

Example

Let's round a negative number to the third decimal place:

print(round(-1.4567, 3))

Result of code execution:

-1.457

See also

  • method floor of module math,
    which rounds a number down
  • method ceil of module math,
    which rounds a number up
byenru