Having looked at three other formatting questions here with tens of answers each, I don't see anyone telling how to print a float 9.9 as 09.90 and 10 as 10.00 using the same f-string:
x = 9.9
print(f"{x:02.2f}") # Should print 09.90
x = 10
print(f"{x:02.2f}") # Should print 10.00
The question is what should replace :02.2f above?
You are looking for
print(f"{x:05.2f}")
The number before the radix point is the total number of characters in the field, not the number to appear before the radix point.
Documentation
f'{x:.2f}'
will print x to two decimal places.
f'{x:05.2f}'
will print x to two decimal places, with five total characters (including the decimal point)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With