print(round(1.365,2)) #1.36 没进一
print('%.2f'%1.365)
print(round(1.3651,2)) #1.37 对的
print('%.2f'%1.3651)
print(round(1.465,2)) #1.47 对的
print('%.2f'%1.465)
没想到什么好办法,先改写了一下
def round_rewrite(data,i=0):
四舍五入,解决round(7.35)=7.3的问题
:param data:
:param i: 保留的位数,默认0
:return:
if not isinstance(i,int): #i不是整数报错
raise TypeError('the second param must be int')
else:
mi = 10**i
datax = data*mi
f = datax - int(datax)
if f >=0.5:
res = (int(datax)+1)/mi
elif f <=-0.5:
res = (int(datax)-1)/mi
else:
res = int(datax)/mi
if i <= 0:
res = int(res)
return res
data = 1.365
print(round(data,2))
print(round_rewrite(data,2))