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
1
  • good answer on stackoverflow here Commented May 18, 2018 at 6:29

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

3

You can do it by

In [11]: x = 5

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

In [13]:

Comments

2

Your answer was correct. you just misplaced the colon:

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

 #output:
'5.00'

;)

2 Comments

what notation is this? In [14]:
Its a line number from an ipython console

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.