ScanSkill

round

This round() function returns a floating-point number rounded to a specified number of decimal places.

Syntax

round(number, digits)

Here,

  • number: Required. A number(integer or float)
  • digits: Optional. An integer specifying the number of decimal places. If omitted, defaults to zero.

Note: Values are rounded to the closest multiple of 10 to the power minus decimal places; if two multiples are equally close, rounding is done away from 0 (so. for example, round(0.5) is 1.0 and round(-0.5) is -1.0).

Examples

>>> round(3.333, 1)
3.3
>>> round(2.675, 2)
2.67 # this is not correct in proper arithmetic
>>> round(0, 1)
0.0

Here, The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. It is because most decimal fractions can’t be represented exactly as a float.