0

How to convert a list into list with 5 decimal places?

 list1 = [0.23400000000, 0.3456222222, 0.1278234, 0.78433333]

Expecting:

 list1 = [0.23400, 0.34562, 0.12782, 0.78433]

I don not expect 0.34562 to be 0.34561, i need to preserve values as it is but with 5 decimal points.

m00am
  • 5,910
  • 11
  • 53
  • 69
Explore_SDN
  • 217
  • 1
  • 3
  • 10

2 Answers2

1

I don't believe you can get a 0.23400 float. The closest solution I can offer is:

list1 = [0.23400000000,0.3456222222,0.1278234,0.78433333]
list1 = [round(i,5) for i in list1]
>>> print(list1)
[0.234, 0.34562, 0.12782, 0.78433]

I don't know why you would need a float of 0.23400 as it doesn't provide any additional mathematical significance to 0.234. Unless what you are really looking for is a string.

Alex McLean
  • 2,524
  • 5
  • 30
  • 53
1

You could use the decimal module:

import decimal
decimal.getcontext.prec() = 5 # Use 5 decimal places
list1 = [0.23400000000,0.3456222222,0.1278234,0.78433333]
list2 = [decimal.Decimal(x) + decimal.Decimal(0) for x in list1] # addition is necessary to trigger rounding
print(list2) # [Decimal('0.23400'), Decimal('0.34562'), Decimal('0.12782'), Decimal('0.78433')]
Ruth Franklin
  • 365
  • 3
  • 2