0

I have a float 17.3743 and I need to put in the format 17.37, using "{:.2f}".format(17.3743) works fine, but when the input have only one digit then it does nothing instead of fixing 2 decimais points, for example 13.3 do not result in 13.30 as I really needed. How can I solve this?

Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • maybe this helps: https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python – mrxra Jun 19 '20 at 18:02
  • 4
    I've just tried `print("{:.2f}".format(13.3))` and it DID result in 13.30 exactly the way you want. Full example? – Boris Lipschitz Jun 19 '20 at 18:05
  • Does this answer your question? [How to format a floating number to fixed width in Python](https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python) – Harshal Parekh Jun 19 '20 at 18:14

2 Answers2

2

Looks like your suggestion works fine to me:

>>> print("{:.2f}".format(123.1123123123))
123.11
>>> print("{:.2f}".format(123.1))
123.10

Taken straight from the Python 3.8 IDLE.

What's not working about it for you? What's the context you are trying to use it in?

It seems like something else might be your problem.

Hanse00
  • 54
  • 4
0

You can simply use format function like below:

format(number, '.2f')
Kurosh Ghanizadeh
  • 539
  • 1
  • 4
  • 16