How to round each item in a list of floats to 2 decimal places?

Python

Python Problem Overview


I have a list which consists of float values but they're too detailed to proceed. I know we can shorten them by using the ("%.f" % variable) operator, like:

result = [359.70000000000005]
result = "%.2f" % result
result = [359.70]

My question is how can I turn a list of values into their rounded equivalents without using an iterator. I've tried something, but it throws a TypeError:

list = [0.30000000000000004, 0.5, 0.20000000000000001]
list = "%.2f" % list
TypeError: not all arguments converted during string formatting

How can I provide a clean list like:

list = [0.30, 0.5, 0.20]

Python Solutions


Solution 1 - Python

"%.2f" does not return a clean float. It returns a string representing this float with two decimals.

my_list = [0.30000000000000004, 0.5, 0.20000000000000001]
my_formatted_list = [ '%.2f' % elem for elem in my_list ]

returns:

['0.30', '0.50', '0.20']

Also, don't call your variable list. This is a reserved word for list creation. Use some other name, for example my_list.

If you want to obtain [0.30, 0.5, 0.20] (or at least the floats that are the closest possible), you can try this:

my_rounded_list = [ round(elem, 2) for elem in my_list ]

returns:

[0.29999999999999999, 0.5, 0.20000000000000001]

Solution 2 - Python

If you really want an iterator-free solution, you can use numpy and its array round function.

import numpy as np
myList = list(np.around(np.array(myList),2))

Solution 3 - Python

You might want to look at Python's decimal module, which can make using floating point numbers and doing arithmetic with them a lot more intuitive. Here's a trivial example of one way of using it to "clean up" your list values:

>>> from decimal import *
>>> mylist = [0.30000000000000004, 0.5, 0.20000000000000001]
>>> getcontext().prec = 2
>>> ["%.2f" % e for e in mylist]
['0.30', '0.50', '0.20']
>>> [Decimal("%.2f" % e) for e in mylist]
[Decimal('0.30'), Decimal('0.50'), Decimal('0.20')]
>>> data = [float(Decimal("%.2f" % e)) for e in mylist]
>>> data
[0.3, 0.5, 0.2]

Solution 4 - Python

mylist = [0.30000000000000004, 0.5, 0.20000000000000001]
myRoundedList =  [round(x,2) for x in mylist] 
# [0.3, 0.5, 0.2]

Solution 5 - Python

You can use the built-in map along with a lambda expression:

my_list = [0.2111111111, 0.5, 0.3777777777]
my_list_rounded = list(map(lambda x: round(x, ndigits=2), my_list))
my_list_rounded                                                                                                                                                                                                                 
Out[3]: [0.21, 0.5, 0.38]

Alternatively you could also create a named function for the rounding up to a specific digit using partial from the functools module for working with higher order functions:

from functools import partial

my_list = [0.2111111111, 0.5, 0.3777777777]
round_2digits = partial(round, ndigits=2)
my_list_rounded = list(map(round_2digits, my_list))
my_list_rounded                                                                                                                                                                                                                 
Out[6]: [0.21, 0.5, 0.38]

Solution 6 - Python

Another option which doesn't require numpy is:

precision = 2  
myRoundedList = [int(elem*(10**precision)+delta)/(10.0**precision) for elem in myList]

# delta=0 for floor
# delta = 0.5 for round
# delta = 1 for ceil

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFishView Question on Stackoverflow
Solution 1 - PythoneumiroView Answer on Stackoverflow
Solution 2 - Pythonjohn ktejikView Answer on Stackoverflow
Solution 3 - PythonmartineauView Answer on Stackoverflow
Solution 4 - PythonSilvia42View Answer on Stackoverflow
Solution 5 - PythonjojoView Answer on Stackoverflow
Solution 6 - PythonAmit OvedView Answer on Stackoverflow