# coding=utf-8
class Special():
def __init__(self):
pass
def roundS(self,num,digit=2):
try:
num=float(num)#判断输入是否为数字
digit = int(digit)#判断输入是否为数字
num = str(num)
if "." in num:
list= num.split('.')
if digit >0:
if len(list[1])>=digit+1:
if list[1][digit] < '5':#判断digit+1位上的数是否小于5
result = float(list[0] + '.' + list[1][0:digit])
elif list[1][digit] <= '9':#判断digit+1位上的数是否大于等于5
str1=""
for i in range(0,digit):
str1=str1+list[1][i]
fl = "0."
for i in range(0,digit-1):
fl=fl+"0"
fl=float(fl+"1")
result = float(list[0] + '.' + str1) + fl
result=self.roundN(result,digit)
return result
else: #小数部分位数小于了指定位数
endnum= ""
for i in range(0, digit - len(list[1])):
endnum= endnum + "0"
str1 = ""
for i in range(0, len(list[1])):
str1 = str1 + list[1][i]
result =list[0] + '.' + str1+endnum
return result
elif digit==0:
if int(list[1][0])>=5:
return int(list[0])+1
else:
return list[0]
else:
digit=str(digit).replace("-",'')
digit=int(digit)
if len(list[0])<=digit:
return "Please re-enter, the input content is not valid conditions"
elif len(list[0])>digit:
str2=list[0]
t = ''
for i in range(0, digit):
t = t + '0'
print str2[-digit]
if str2[-digit]>=5:
str3 = str2[-len(str2):-digit]
str3 =str(int(str3)+1)+t
return int(str3)
else:
str3 = str2[-len(str2):-(digit + 1)]+t
return int(str3)
else: #不是小数的整数,在其后添加指定位数的零
endnum = ""
for i in range(0, digit):
if i ==0:
endnum="."
endnum = endnum + "0"
result=num+endnum
return result
except ValueError:
return "Please enter real number"
def roundN(self, num, digit=2):
try:
num = float(num) # 判断输入是否为数字
digit = int(digit) # 判断输入是否为数字
num = str(num)
if "." in num:
list = num.split('.')
if digit > 0:
if len(list[1]) >= digit:
str1 = ""
for i in range(0, digit):
str1 = str1 + list[1][i]
result = list[0] + '.' + str1
return result
else: # 小数部分位数小于了指定位数
endnum = ""
for i in range(0, digit - len(list[1])):
endnum = endnum + "0"
str1 = ""
for i in range(0, len(list[1])):
str1 = str1 + list[1][i]
result = list[0] + '.' + str1 + endnum
return result
elif digit == 0:
return list[0]
else:
digit = str(digit).replace("-", '')
digit = int(digit)
if len(list[0]) <= digit:
return "Please re-enter, the input content is not valid conditions"
elif len(list[0]) > digit:
str2 = list[0]
t=''
for i in range(0,digit):
t=t+'0'
str3=str2[-len(str2):-digit]+t
return int(str3)
else: # 不是小数的整数,在其后添加指定位数的零
endnum = ""
for i in range(0, digit):
if i == 0:
endnum = "."
endnum = endnum + "0"
result = num + endnum
return result
except ValueError:
return "Please enter real number"
def Datas_Handle(self, arg1, arg2, default1=True, default2=False, num=2):
"""
default1--控制是否进行先判断数据是否为数字,default1=True表示:如果为数字型就进行取取指定位数进行比较
default1=False表示:表示直接当普通字符串处理
default2--如果为数字类型,且进行数字比较,是否进行四舍五入
num--如果进行四舍五入,就按指定num位数进行四舍五入
"""
arg1 = str(arg1).strip().strip("'")
arg2 = str(arg2).strip().strip("'")
if default1:
try:
float(arg1)
float(arg2)
if not default2:
if ('.' not in str(arg1) or '.' not in str(arg1).encode('utf-8')) and (
'.' not in str(arg2) or '.' not in str(arg2).encode('utf-8')):
pass
else:
arg1 = self.roundN(arg1, num)
arg2 = self.roundN(arg2, num)
if default2:
if ('.' not in str(arg1) or '.' not in str(arg1).encode('utf-8')) and (
'.' not in str(arg2) or '.' not in str(arg2).encode('utf-8')):
pass
else:
arg1 = self.roundS(arg1, num)
arg2 = self.roundS(arg2, num)
except:
pass
return arg1, arg2
#####################################################################################
if __name__ == '__main__':
a=Special()
print a.roundN('499.5004995', 2)
print type(a.roundN('499.5004995', 2))
print a.roundS('499.494995', 2)
print a.roundN('499.5000', 2)
# print a.roundN(5678.444,0)
# print a.roundN(5678.544, 0)
# print a.roundN(5678.1255, 3)
# print a.roundN(5678.124,2)
# print a.roundN(5678, 3)
# print a.roundN(5678.53, 0)
# print a.roundN(5678.53, 4)
# print a.roundN(5678, 0)
# print a.roundN(4444444.4444, -7)
# print a.roundS(12345678.4444, 0)
# print a.roundS(0000008.4444, 0)
# print a.roundN("1.333 ", 2)
# print a.roundN("333 ", 3)
print a.Datas_Handle(3434.444,55555.555)[1]
Python中处理四舍五入问题,以字符串的方式处理
最新推荐文章于 2024-07-02 15:20:43 发布