-2

I wanted to keep two decimal places after the decimal and keep the trailing 0s for example. 17.7 I want it to be 17.70, or 22.00. So, I have a list of numbers I want to return the maximum and round so that it fits that format.

list = [11, 12.3, 14, 200.33, 49.08, 207.2]

a = max(round(list), 2)

This is what I have done, but it still does not round all the points separately and keep the 2 decimal places. I am not tied to keeping round inside of max, I was just experimenting.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Hollywood
  • 3
  • 2
  • 2
    The zeros are always there (just like leading zeros). What you're looking for is how to format the string representation to show the trailing zeros. – Woodford Aug 25 '21 at 17:23
  • One solution is to do the following: [math.floor(x * 100)/100 for x in list] This will round everything to 2 decimal places for you. – Eric Taurone Aug 25 '21 at 17:34

1 Answers1

0

Short answer, you can't. Unless you modify the underlying object, you cannot change its representation. What you might want is a string formatted to look like this. Here are two approaches:

my_list = [11, 12.3, 14, 200.33, 49.08, 207.2]
['%.2f'%i for i in my_list]

output:

['11.00', '12.30', '14.00', '200.33', '49.08', '207.20']

f-strings (python >3.6):

[f'{i:.2f}' for i in my_list]

NB. do NOT call a list "list", this is already a python builtin

mozway
  • 194,879
  • 13
  • 39
  • 75