Limiting floats to two decimal points

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

    The method I use is that of string slicing. It's relatively quick and simple.

    First, convert the float to a string, the choose the length you would like it to be.

    float = str(float)[:5]
    

    In the single line above, we've converted the value to a string, then kept the string only to its first four digits or characters (inclusive).

    Hope that helps!

提交回复
热议问题