【发布时间】:2020-06-02 10:20:04
【问题描述】:
python 2 中有没有可以做到这一点的函数?
1234 -> round(1234, 2) = 1200
1234 -> round(1234, 3) = 1230
12.34 -> round(12.34, 3) = 12.3
基本上第二个数字表示数字的精度,后面的一切都应该四舍五入。
根据我想出的评论:
def round_to_precision(x, precision):
return int(round(x / float(10 ** precision))) * 10 ** precision
但这仍然是错误的,因为我不知道数字的大小。
【问题讨论】:
-
检查this 网站。该算法适用于所有数字
-
谢谢。我做了修改,但还是有问题。
标签: python python-2.7