Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format float number in python? [duplicate]

Tags:

python

format

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
like image 539
Manil Puri Manil Avatar asked May 18 '18 06:05

Manil Puri Manil


3 Answers

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

like image 100
Salman Avatar answered Sep 28 '22 01:09

Salman


You can do it by

In [11]: x = 5

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

In [13]:
like image 23
Nishant Nawarkhede Avatar answered Sep 27 '22 23:09

Nishant Nawarkhede


Your answer was correct. you just misplaced the colon:

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

 #output:
'5.00'

;)

like image 20
Rachit kapadia Avatar answered Sep 27 '22 23:09

Rachit kapadia