9

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
Manil Puri Manil
  • 117
  • 1
  • 1
  • 6

3 Answers3

6

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

Salman
  • 924
  • 1
  • 7
  • 20
3

You can do it by

In [11]: x = 5

In [12]: print("%.2f" % x)
5.00

In [13]:
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
2

Your answer was correct. you just misplaced the colon:

print "{:.2f}".format(5.0)

 #output:
'5.00'

;)

Rachit kapadia
  • 705
  • 7
  • 18