Display a float with two decimal places in Python

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

    String Formatting:

    a = 6.789809823
    print('%.2f' %a)
    

    OR

    print ("{0:.2f}".format(a)) 
    

    Round Function can be used:

    print(round(a, 2))
    

    Good thing about round() is that, we can store this result to another variable, and then use it for other purposes.

    b = round(a, 2)
    print(b)
    

提交回复
热议问题