Display a float with two decimal places in Python

前端 未结 11 2179
我在风中等你
我在风中等你 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:38

    If you actually want to change the number itself instead of only displaying it differently use format()

    Format it to 2 decimal places:

    format(value, '.2f')
    

    example:

    >>> format(5.00000, '.2f')
    '5.00'
    

提交回复
热议问题