# How to round numbers in Python

## Problem –

You want to round a floating point value to a fixed number of decimal places.

## Solution –

For rounding in python you can use the round(value, ndigits) function in python. It takes two parameters, the value and the number of decimal places you want to round.

In [1]: round(2.33,1)
Out[1]: 2.3

In [2]: round(54.2345, 2)
Out[2]: 54.23

In [3]: round(2.7893,3)
Out[3]: 2.789


But if your goal is to just format the number for display, you can use the format function.


In [4]: format(2.33, '0.1f')
Out[4]: '2.3'

In [5]: format(54.2345,'0.2f')
Out[5]: '54.23'

In [6]: 'after formatting - {:0.3f}'.format(2.7893)
Out[6]: 'after formatting - 2.789'

Rating: 1 out of 5.