python中round()函数精度问题python数据精度问题:
import numpy as np地理大师
print(np.around(0.155, 2))
print(np.around(0.005, 2))
⼿写实现四舍五⼊:
def round_up(number,power=0):
"""
实现精确四舍五⼊,包含正、负⼩数多种场景
:
param number: 需要四舍五⼊的⼩数女性职场
:param power: 四舍五⼊位数,⽀持0-∞
:return: 返回四舍五⼊后的结果木瓜的吃法和做法
"""
digit = 10 ** power
num2 = float(int(number * digit))
# 处理正数,power不为0的情况
if number>=0 and power !=0:
身先死
tag = number * digit - num2 + 1 / (digit * 10)
if tag>=0.5:胡亥怎么死的
return (num2+1)/digit
el:
return num2/digit制约的近义词
# 处理正数,power为0取整的情况
elif number>=0 and power==0 :不辍
tag = number * digit - int(number)
if tag >= 0.5:
return (num2 + 1) / digit
el:
return num2 / digit
# 处理负数,power为0取整的情况
elif power==0 and number<0:
tag = number * digit - int(number)
if tag <= -0.5:
return (num2 - 1) / digit美丽新城
el:
return num2 / digit
# 处理负数,power不为0的情况
el:
tag = number * digit - num2 - 1 / (digit * 10)
if tag <= -0.5:
return (num2-1)/digit
el:
return num2/digit