python中round函数用法
round函数用于四舍五入的操作,其语法如下:
round(number[, ndigits])
参数说明:
- number:要进行四舍五入的数字,可以是整数、浮点数等。
- ndigits:保留小数位数,默认为0。如果为正数,则四舍五入到小数点后指定位数;如果为负数,则四舍五入到整数位指定的位数。
示例:
print(round(3.1415926)) # 输出3
print(round(3.1415926, 2)) # 输出3.14
print(round(3.1415926, -1)) # 输出0.0
print(round(123456, -3)) # 输出123000
注意:round函数的返回值是一个浮点数或整数,而不是一个字符串。如果需要将其转化为字符串,可以使用str函数。
原文地址: https://www.cveoy.top/t/topic/bu0T 著作权归作者所有。请勿转载和采集!