首页 > 解决方案 > Python:圆形浮点数保持0

问题描述

我只需要在 Python 中舍入浮点数的最后 2 个正数

前任:

这可能以像圆形这样的简单方式吗?

标签: pythonrounding

解决方案


我认为您的示例是错误的,但只需根据需要调整代码:

# this code doesn't work if some are defined as 0.123 and others as .123
a = 0.000000302329303
nb_characters = len(str(a))
rounding_expr = "%%.%sf" % (nb_characters - 4)
rounded_a = float(rounding_expr % a)

推荐阅读