python输出数值保留两位小数
作者:野牛程序员:2023-12-12 08:57:59python阅读 2685
使用Python输出数值并保留两位小数,可以使用 round()
函数或者字符串格式化来实现。
使用 round()
函数:
num = 3.14159 result = round(num, 2) print(result)
使用字符串格式化:
num = 3.14159 result = "{:.2f}".format(num) print(result)
这两种方法都会将数字 3.14159
输出为保留两位小数的格式。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:c++如何判断输入一个数为整数
- 下一篇:Python用sum函数计算总和