# Round a list of floats to 2 decimal places in Python Last updated: Sep 1, 2022 Photo from Unsplash

## Round a list of floats to 2 decimal places in Python#

To round a list of floats to 2 decimal places:

1. Use a list comprehension to iterate over the list.
2. Use the `round()` function to round each float to 2 decimal places.
3. The new list will contain floating-point numbers rounded to 2 decimals.
main.py
```Copied!```list_of_floats = [2.4834843583, 4.28384291, 8.47989238]

# ✅ Round list of floats to 2 decimal places (round())
result = [round(item, 2) for item in list_of_floats]
print(result)  # 👉️ [2.48, 4.28, 8.48]

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

# ✅ Format list of floats to 2 decimal places (f-string)

result = [f'{item:.2f}' for item in list_of_floats]
print(result)  # 👉️ ['2.48', '4.28', '8.48']

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

# ✅ Round list of floats to 2 decimal places (numpy.around())

import numpy as np

result = list(np.around(list_of_floats, 2))
print(result)  # 👉️ [2.48, 4.28, 8.48]
``````

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

main.py
```Copied!```list_of_floats = [2.4834843583, 4.28384291, 8.47989238]

result = [round(item, 2) for item in list_of_floats]
print(result)  # 👉️ [2.48, 4.28, 8.48]
``````
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 the `round()` function to round the current float to 2 decimal places.

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 list of floats to 2 decimal places using f-string#

To round a list of floats to 2 decimal places:

1. Use a list comprehension to iterate over the list.
2. Use a formatted-string literal to round each float to 2 decimal places.
3. The new list will contain floating-point numbers rounded to 2 decimals.
main.py
```Copied!```list_of_floats = [2.4834843583, 4.28384291, 8.47989238]

result = [f'{item:.2f}' for item in list_of_floats]
print(result)  # 👉️ ['2.48', '4.28', '8.48']
``````
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 = 2.4834843583

print(f'{my_float:.2f}')  # 👉️ 2.48
print(f'{my_float:.3f}')  # 👉️ 2.483
``````

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!```list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
number_of_decimals = 2

result = [f'{item:.{number_of_decimals}f}' for item in list_of_floats]
print(result)  # 👉️ ['2.48', '4.28', '8.48']
``````

If you use numpy, you can use the `numpy.around()` method.

## Round a list of floats to 2 decimal places using numpy.around()#

To round a list of floats to 2 decimal places:

1. Use the `numpy.around()` method to round each float in the list to 2 decimal places.
2. Use the `list()` class to convert the numpy array to a list.
main.py
```Copied!```import numpy as np

list_of_floats = [2.4834843583, 4.28384291, 8.47989238]

result = list(np.around(list_of_floats, 2))
print(result)  # 👉️ [2.48, 4.28, 8.48]
``````

The numpy.around method takes an array-like object and rounds its items to the given number of decimals.

The method returns a numpy array, so we had to use the `list()` class to convert the result to a native Python list.

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.