# Round a float to 3 decimal places in Python Last updated: Sep 1, 2022 Photo from Unsplash

## Round a float to 3 decimal places in Python#

Use the `round()` function to round a float to 3 decimal places, e.g. `result = round(6.36789, 3)`. The `round()` function will round the floating-point number to 3 decimal places and will return the result.

main.py
```Copied!```my_float = 6.36789

# ✅ round a float to 3 decimal places (round())

result = round(my_float, 3)
print(result)  # 👉️ 6.368

# -------------------------------------

# ✅ round a float to 3 decimal places (f-string)

result = f'{my_float:.3f}'
print(result)  # 👉️ '6.368'

# -------------------------------------

# ✅ round list of floats to 3 decimal places

list_of_floats = [2.298438438, 4.5848548, 8.8347347]

result = [f'{item:.3f}' for item in list_of_floats]
print(result)  # ['2.298', '4.585', '8.835']
``````

The first example uses the `round()` function to round a floating-point number to 3 decimal places.

main.py
```Copied!```my_float = 6.36789

result = round(my_float, 3)
print(result)  # 👉️ 6.368
``````

The round function takes the following 2 parameters:

NameDescription
`number`the number to round to `ndigits` precision after the decimal
`ndigits`the number of digits after the decimal, the number should have after the operation (optional)

The `round` function returns the number rounded to `ndigits` precision after the decimal point.

Alternatively, you can use a formatted string literal.

## Round a float to 3 decimal places using f-string#

Use a formatted string literal to round a float to 3 decimal places, e.g. `result = f'{my_float:.3f}'`. The formatted string literal will round the float to 3 decimal places and will return the result.

main.py
```Copied!```my_float = 6.36789

result = f'{my_float:.3f}'
print(result)  # 👉️ '6.368'

number_of_decimals = 3

result = f'{my_float:.{number_of_decimals}f}'
print(result)  # 👉️ '6.368'
``````
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with `f`.

Make sure to wrap expressions in curly braces - `{expression}`.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
```Copied!```my_float = 6.36789

# 👇️ Rounded to 2 decimals: 6.37
print(f'Rounded to 2 decimals: {my_float:.2f}')

# 👇️ Rounded to 3 decimals: 6.368
print(f'Rounded to 3 decimals: {my_float:.3f}')
``````

The digit after the period is the number of decimal places the float should have.

If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.

main.py
```Copied!```my_float = 6.36789

number_of_decimals = 3

result = f'{my_float:.{number_of_decimals}f}'
print(result)  # 👉️ '6.368'
``````

If you need to round a list of floating-point numbers to 3 decimal places, use a list comprehension.

main.py
```Copied!```my_float = 6.36789

list_of_floats = [2.298438438, 4.5848548, 8.8347347]

result = [f'{item:.3f}' for item in list_of_floats]
print(result)  # 👉️ ['2.298', '4.585', '8.835']
``````

We used a list comprehension to iterate over the list of floating-point numbers.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to round the current float to 3 decimal places and return the result.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer. You can use the search field on my Home Page to filter through all of my articles.