0

Using the f-string floating formatting I get the following results:

f"{1.34 :.2f}"  # 1.34
f"{1.30 :.2f}"  # 1.30
f"{1.00 :.2f}"  # 1.00

What I want however is to remove all trailing zeros:

f"{1.34 :.2f}"  # 1.34
f"{1.30 :.2f}"  # 1.3
f"{1.00 :.2f}"  # 1

Using complex functions I can strip zeros and possibly the decimal sign. But it should be a common f-string notation, but I can't find it.

Johan Tuls
  • 105
  • 7
  • 1
    There isn't anything built in https://stackoverflow.com/questions/66100708/how-to-skip-trailing-zeroes-from-decimal-in-pythons-f-strings – Guy Oct 20 '22 at 06:54
  • Okay, thank for the reply. Guess I'll keep the trailing zeros for now. – Johan Tuls Oct 20 '22 at 07:15

1 Answers1

0

You can try this

my_float = 69.880

result = f'{str(my_float).rstrip("0").rstrip(".") if "." in str(my_float) else my_float}'

print(result)