如何让算法自动以 "smart" 的方式轮回
How to have an algorithm round in a "smart" way automatically
我想将代码中的数字四舍五入,但要以适应每个值的方式进行。
例如我想要一个舍入算法 return :
- 0.999999 它应该 return 1
- 0.0749999 它应该 return 0.075
- 0.006599 它应该 return0.0066
等等...
我事先不知道位数(这是我的问题)
我正在考虑使用字符串来查找 9 的位置(或计算 0),但我正在考虑为此付出很多努力?
如果您知道任何方法(如果可能,没有高级库),我将不胜感激。
谢谢。
有点复杂。但是,它有效。请确保结果是你想要的。我想你可以从代码中理解如何对数字进行四舍五入。
def clear9(numstr):
liststr = list(numstr)
for index in range(len(liststr)-1,-1,-1):
if liststr[index] == '.': continue
if liststr[index] == '9':
liststr[index] = '0'
if index == 0:
liststr.insert(0, '1')
else:
if index != len(liststr)-1:
liststr[index] = str(int(liststr[index])+1)
break
numstr = ''
for item in liststr:
numstr += item
return numstr
def myround(num):
numstr = str(num)
numstr = clear9(numstr)
return float(numstr)
print (myround(9.05))
print (myround(9.999999))
print (myround(0.999999))
print (myround(0.0749999))
print (myround(0.006599))
print (myround(0.00659923))
print (myround(0.09659923))
print (myround(-0.00659923))
9.05
10.0
1.0
0.075
0.0066
0.00659923
0.09659923
-0.00659923
import math
def round_(number):
dist = int(math.log10(abs(number))) #number of zeros after decimal point
return (round(number, abs(dist) + 2) if dist != 0 else round(number))
print(round_(0.999999))
print(round_(0.0749999))
print(round_(0.006599))
print(round_(-0.00043565))
输出:
1
0.075
0.0066
-0.00044
处理浮点数很棘手。你想做一种以 10 为底的 round-off,但浮点数以 2 为底。
所以我建议使用 decimal
模块,它可以精确地表示实数,而不是 base-2 浮点数。:
from decimal import Decimal
def myround(num):
dec = Decimal(num)
adj = abs(dec.adjusted())+1
return round(num, adj)
查看 Decimal.adjusted()
的 documentation 以了解其工作原理。
一个测试:
In [1]: from decimal import Decimal
In [2]: def myround(num):
...: dec = Decimal(num)
...: adj = abs(dec.adjusted())+1
...: return round(num, adj)
...:
In [3]: myround(0.999999)
Out[3]: 1.0
In [4]: myround(0.006599)
Out[4]: 0.0066
In [5]: myround(0.0749999)
Out[5]: 0.075
我想将代码中的数字四舍五入,但要以适应每个值的方式进行。
例如我想要一个舍入算法 return :
- 0.999999 它应该 return 1
- 0.0749999 它应该 return 0.075
- 0.006599 它应该 return0.0066 等等...
我事先不知道位数(这是我的问题)
我正在考虑使用字符串来查找 9 的位置(或计算 0),但我正在考虑为此付出很多努力?
如果您知道任何方法(如果可能,没有高级库),我将不胜感激。
谢谢。
有点复杂。但是,它有效。请确保结果是你想要的。我想你可以从代码中理解如何对数字进行四舍五入。
def clear9(numstr):
liststr = list(numstr)
for index in range(len(liststr)-1,-1,-1):
if liststr[index] == '.': continue
if liststr[index] == '9':
liststr[index] = '0'
if index == 0:
liststr.insert(0, '1')
else:
if index != len(liststr)-1:
liststr[index] = str(int(liststr[index])+1)
break
numstr = ''
for item in liststr:
numstr += item
return numstr
def myround(num):
numstr = str(num)
numstr = clear9(numstr)
return float(numstr)
print (myround(9.05))
print (myround(9.999999))
print (myround(0.999999))
print (myround(0.0749999))
print (myround(0.006599))
print (myround(0.00659923))
print (myround(0.09659923))
print (myround(-0.00659923))
9.05
10.0
1.0
0.075
0.0066
0.00659923
0.09659923
-0.00659923
import math
def round_(number):
dist = int(math.log10(abs(number))) #number of zeros after decimal point
return (round(number, abs(dist) + 2) if dist != 0 else round(number))
print(round_(0.999999))
print(round_(0.0749999))
print(round_(0.006599))
print(round_(-0.00043565))
输出:
1
0.075
0.0066
-0.00044
处理浮点数很棘手。你想做一种以 10 为底的 round-off,但浮点数以 2 为底。
所以我建议使用 decimal
模块,它可以精确地表示实数,而不是 base-2 浮点数。:
from decimal import Decimal
def myround(num):
dec = Decimal(num)
adj = abs(dec.adjusted())+1
return round(num, adj)
查看 Decimal.adjusted()
的 documentation 以了解其工作原理。
一个测试:
In [1]: from decimal import Decimal
In [2]: def myround(num):
...: dec = Decimal(num)
...: adj = abs(dec.adjusted())+1
...: return round(num, adj)
...:
In [3]: myround(0.999999)
Out[3]: 1.0
In [4]: myround(0.006599)
Out[4]: 0.0066
In [5]: myround(0.0749999)
Out[5]: 0.075