How can I make sure that I all decimal places even if 0 are shown

Question:

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

for item in my_rounded_list:
print(" ", item[0], "|", item[1], "|", item[2], "|")

How can I show all decimal places as part of my list

Asked By: DandyLione

||

Answers:

Use string formatting:

print("   x    | sin(x) | cos(x)")
print("--------|--------|--------")

for item in my_list:
    print(" |".join([f'{x: 7.3f}' for x in item]))

Output:

   x    | sin(x) | cos(x)
--------|--------|--------
  0.000 |  0.000 |  1.000
  0.314 |  0.309 |  0.951
  0.628 |  0.588 |  0.809
  0.942 |  0.809 |  0.588
  1.257 |  0.951 |  0.309
  1.571 |  1.000 |  0.000
  1.885 |  0.951 | -0.309
  2.199 |  0.809 | -0.588
  2.513 |  0.588 | -0.809
  2.827 |  0.309 | -0.951
  3.142 |  0.000 | -1.000
Answered By: mozway
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.