【发布时间】:2016-01-23 19:34:15
【问题描述】:
我正在尝试编写一个函数来将浮点数四舍五入到 n 位小数。该函数可以接受一个或两个参数。如果只有一个参数,则数字应四舍五入到小数点后两位。
这是我到目前为止所得到的:
def roundno(num,point=2):
import math
x=1*(math.pow(10,-point))
round=0
while (num>x):
while(num>0):
round+=num/10
num=num/10
round*=10
round+=num/10
num=num/10
round*=0.1
return round
我每次都得到无穷大作为输出......我哪里出错了?
【问题讨论】:
标签: python floating-point decimal rounding