如何字符串格式,%
或.format()
,一个要四舍五入并显示到 10 秒或 100 秒的位置的浮点数?
像 4552.33 到 4550 到 10s 的地方或 4600 到 100 的地方?
使用内置函数 round
,
>>> import math
>>> f = 4552.33
>>> int(round(f, -int(math.log10(10))))
4550
>>> int(round(f, -int(math.log10(100))))
4600