How to turn a float number like 293.4662543 into 293.47 in python?

前端 未结 8 1913
小蘑菇
小蘑菇 2021-01-01 10:19

How to shorten the float result I got? I only need 2 digits after the dot. Sorry I really don\'t know how to explain this better in English...

Thanks

8条回答
  •  渐次进展
    2021-01-01 11:04

    I hope this will help.

    def do(*args):
        formattedList = [float("{:.2f}".format(num)) for num in args]
        _result =(sum(formattedList))
        result = round(_result,2)
        return result
    
    
    print(do(23.2332,45.24567,67,54.27))
    

    Result:

    189.75
    

提交回复
热议问题