Limiting floats to two decimal points

前端 未结 28 3147
你的背包
你的背包 2020-11-21 04:57

I want a to be rounded to 13.95.

>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999

The ro

28条回答
  •  长情又很酷
    2020-11-21 05:03

    As @Matt pointed out, Python 3.6 provides f-strings, and they can also use nested parameters:

    value = 2.34558
    precision = 2
    width = 4
    
    print(f'result: {value:{width}.{precision}f}')
    

    which will display result: 2.35

提交回复
热议问题