Limiting floats to two decimal points

前端 未结 28 3208
你的背包
你的背包 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:12

    I feel that the simplest approach is to use the format() function.

    For example:

    a = 13.949999999999999
    format(a, '.2f')
    
    13.95
    

    This produces a float number as a string rounded to two decimal points.

提交回复
热议问题