可以使用 Python 的内置函数 round() 来保留两位小数,例如:

num = 3.1415926
rounded_num = round(num, 2)
print(rounded_num)

输出:

3.14

此外,也可以使用字符串格式化来保留两位小数,例如:

num = 3.1415926
formatted_num = "{:.2f}".format(num)
print(formatted_num)

输出:

3.14
python 保留两位小数

原文地址: http://www.cveoy.top/t/topic/g0Rv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录