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?
Asked
Active
Viewed 389 times
0

Olvin Roght
- 7,677
- 2
- 16
- 35

user276684
- 3
- 1
-
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
-
4I'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 Answers
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
-
1Yep, just tried myself on 3.8.2 and 2.7.13, both work exactly the way OP wants with the exact format string the OP offers. – Boris Lipschitz Jun 19 '20 at 18:12
-
As an addendum, it works without any decimals at all: `>>> print("{:.2f}".format(123)) 123.00 ` – Hanse00 Jun 19 '20 at 18:34