How can I format a decimal to always show 2 decimal places?

前端 未结 12 2117
有刺的猬
有刺的猬 2020-11-22 17:31

I want to display:

49 as 49.00

and:

54.9 as 54.90

Regardless of the length of the decimal

12条回答
  •  情深已故
    2020-11-22 17:47

    >>> print "{:.2f}".format(1.123456)
    1.12
    

    You can change 2 in 2f to any number of decimal points you want to show.

    EDIT:

    From Python3.6, this translates to:

    >>> print(f"{1.1234:.2f}")
    1.12
    

提交回复
热议问题