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

前端 未结 12 1971
有刺的猬
有刺的猬 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:42

    if you have multiple parameters you can use

     print('some string {0:.2f} & {1:.2f}'.format(1.1234,2.345))
     >>> some string 1.12 & 2.35
    
    0 讨论(0)
  • 2020-11-22 17:43

    This is the same solution as you have probably seen already, but by doing it this way it's more clearer:

    >>> num = 3.141592654

    >>> print(f"Number: {num:.2f}")

    0 讨论(0)
  • 2020-11-22 17:46

    You can use the string formatting operator as so:

    num = 49
    x = "%.2f" % num  # x is now the string "49.00"
    

    I'm not sure what you mean by "efficient" -- this is almost certainly not the bottleneck of your application. If your program is running slowly, profile it first to find the hot spots, and then optimize those.

    0 讨论(0)
  • 2020-11-22 17:46

    If you're using this for currency, and also want the value to be seperated by ,'s you can use

    $ {:,.f2}.format(currency_value).

    e.g.:

    currency_value = 1234.50

    $ {:,.f2}.format(currency_value) --> $ 1,234.50

    Here is a bit of code I wrote some time ago:

    print("> At the end of year " + year_string + " total paid is \t$ {:,.2f}".format(total_paid))

    > At the end of year   1  total paid is         $ 43,806.36
    > At the end of year   2  total paid is         $ 87,612.72
    > At the end of year   3  total paid is         $ 131,419.08
    > At the end of year   4  total paid is         $ 175,225.44
    > At the end of year   5  total paid is         $ 219,031.80   <-- Note .80 and not .8
    > At the end of year   6  total paid is         $ 262,838.16
    > At the end of year   7  total paid is         $ 306,644.52
    > At the end of year   8  total paid is         $ 350,450.88
    > At the end of year   9  total paid is         $ 394,257.24
    > At the end of year  10  total paid is         $ 438,063.60   <-- Note .60 and not .6
    > At the end of year  11  total paid is         $ 481,869.96
    > At the end of year  12  total paid is         $ 525,676.32
    > At the end of year  13  total paid is         $ 569,482.68
    > At the end of year  14  total paid is         $ 613,289.04
    > At the end of year  15  total paid is         $ 657,095.40   <-- Note .40 and not .4  
    > At the end of year  16  total paid is         $ 700,901.76
    > At the end of year  17  total paid is         $ 744,708.12
    > At the end of year  18  total paid is         $ 788,514.48
    > At the end of year  19  total paid is         $ 832,320.84
    > At the end of year  20  total paid is         $ 876,127.20   <-- Note .20 and not .2
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 17:49

    In python 3, a way of doing this would be

    '{:.2f}'.format(number)
    
    0 讨论(0)
提交回复
热议问题