Display a float with two decimal places in Python

前端 未结 11 2190
我在风中等你
我在风中等你 2020-11-22 14:36

I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 14:44

    I know it is an old question, but I was struggling finding the answer myself. Here is what I have come up with:

    Python 3:

    >>> num_dict = {'num': 0.123, 'num2': 0.127}
    >>> "{0[num]:.2f}_{0[num2]:.2f}".format(num_dict) 
    0.12_0.13
    

提交回复
热议问题