I want to format my float number with 2 digit after decimal.
>>> x =5.0
>>> y=float("{:0.2f}".format(x))
>>> y
5.0
i want my output in this format:
5.00
For newer version of python you can use:
x = 5.0
print(f' x: {x:.2f}')
out put will be:
x: 5.00
for more about this style see: f-string
You can do it by
In [11]: x = 5
In [12]: print("%.2f" % x)
5.00
In [13]:
Your answer was correct. you just misplaced the colon:
print "{:.2f}".format(5.0)
#output:
'5.00'
;)
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