None 我知道的小数舍入方法可以满足我当前的需要(将字符串从小数赔率转换为小数赔率)

None of the decimal rounding methods I know worked for my current need (converting string from a fractional odds to decimal odds)

a 列表中的值是来自 API.

的数学除法值

要将字符串转换为计算,我使用 eval()

correct_list中的值为本次相加+1.
的最终结果 API 表示这是使用其数据计算的准确结果。

但是当这些除法有几个小数时我遇到了困难,我尝试格式化每个模型以便小数限制为 2,创建的列表与正确的结果不完全匹配:

import math
import decimal
decimal.getcontext().rounding = decimal.ROUND_CEILING

def main():
    a = ['11/10', '29/20', '5/4', '1/2', '10/11', '2/1', '4/7', '11/8', '13/10', '1/8', '4/11', '5/4', '163/100', '11/20', '17/20', '23/20', '6/4', '1/1', '8/5']
    b = []
    correct_list = ['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.57', '2.38', '2.30', '1.13', '1.36', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
    for c in a:
        b.append(f'{round(eval(c)+1, ndigits=2)}')
    print(b)
    if b == correct_list:
        print('Ok. Amazing Match!')

if __name__ == '__main__':
    main()

这些都是我的尝试:

b.append(f'{eval(c)+1:.2f}')
['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.57', '2.38', '2.30', '1.12', '1.36', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
b.append(f'{math.ceil((eval(c)+1) * 100.0) / 100.0:.2f}')
['2.10', '2.46', '2.25', '1.50', '1.91', '3.00', '1.58', '2.38', '2.30', '1.13', '1.37', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
b.append(f'{float(round(decimal.Decimal(str(eval(c)+1)), ndigits=2)):.2f}')
['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.58', '2.38', '2.30', '1.13', '1.37', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
b.append(f'{round(eval(c)+1, ndigits=2):.2f}')
['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.57', '2.38', '2.30', '1.12', '1.36', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']

None 提供了与 API 计算模型完美匹配的结果。

正常 round() 函数无法按您想要的方式处理的唯一情况是它将 0.005 向下舍入为 0.00 而不是向上舍入 0.01 .因此,如果您愿意俗气,只需添加 1.001 而不是 1,然后使用正常舍入。

a = ['11/10', '29/20', '5/4', '1/2', '10/11', '2/1', '4/7', '11/8', '13/10', '1/8', '4/11', '5/4', '163/100', '11/20', '17/20', '23/20', '6/4', '1/1', '8/5']
correct_list = ['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.57', '2.38', '2.30', '1.13', '1.36', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
b = [f'{float.__truediv__(*map(float, c.split("/"))) + 1.001:.2f}' for c in a]
if b == correct_list:
    print('Ok. Amazing Match!')  # succeeds

如果你想使用decimal模块以non-cheesy的方式来做,你需要用Decimal个对象做数学运算,你想要的舍入方法是ROUND_HALF_UP.

import decimal
decimal.getcontext().rounding = decimal.ROUND_HALF_UP

a = ['11/10', '29/20', '5/4', '1/2', '10/11', '2/1', '4/7', '11/8', '13/10', '1/8', '4/11', '5/4', '163/100', '11/20', '17/20', '23/20', '6/4', '1/1', '8/5']
correct_list = ['2.10', '2.45', '2.25', '1.50', '1.91', '3.00', '1.57', '2.38', '2.30', '1.13', '1.36', '2.25', '2.63', '1.55', '1.85', '2.15', '2.50', '2.00', '2.60']
b = []
for c in a:
    n, d = map(int, c.split("/"))
    result = decimal.Decimal(1) + decimal.Decimal(n) / decimal.Decimal(d)
    b.append(f'{result:.2f}')
if b == correct_list:
    print('Ok. Amazing Match!')  # succeeds