我正在将价格表中的价格(乘以百分比)与数据库中存储的当前价格进行比较。
测试:
for recurringFee, plan_rate_id in data:
for offer_name, pc_offer_id, agreement_type, purchase_unit, secondary_license_type, end_customer_type, pc_list_price,pc_erp_price in price_list:
price_list_types.add(type(pc_erp_price).__name__)
pba_db_types.add(type(recurringFee).__name__)
print(price_list_types)
print(pba_db_types)
输出:
{'float', 'int'}
{'Decimal'}
如果价格有变化,我会将db值更新为新价格。
如果数据库中的当前价格为0.45(例如),那么当我执行选择操作时,它始终被读取/存储为:
0.45000000
我相信math.isclose()函数可以为我提供帮助。
math.isclose()为false时,它会触发:
问题现在正在打印到屏幕上,并追加到列表中列出实际更改以及相同的值。
我正在使用.format()打印到屏幕上
我尝试为math.isclose定义一个rel_tol值,但是如果我不指定任何rel_tol,我相信数字已经被正确地比较了
>>> fee = 6.51000000
>>> print(erp_price)
6.5068
>>> math.isclose(fee,erp_price)
False
>>> math.isclose(fee,erp_price,rel_tol=0.01)
True
>>> math.isclose(fee,erp_price,rel_tol=0.001)
True
>>> math.isclose(fee,erp_price,rel_tol=0.0001)
False
>>>
代码段:
if not(math.isclose(pc_list_price,recurringFee)):
print("Addons Markup: \n{}:\n{}\nPlan {}:\nYearly: {}\nOwner: {}\nPrice {} -> {}\n".format(res_name,offer_id,plan_id,yearly,owner_id,float(recurringFee),round(pc_list_price,2)))
writer.writerow([owner_id,currency,plan_id,res_id,plan_rate_id,res_name,offer_id,float(recurringFee),round(pc_list_price,2),value,key])
to_be_updated.append((owner_id,plan_id,plan_rate_id,round(pc_list_price,2),i))
输出:
Addons Markup:
Dynamics 365 Customer Insights Addnl Profiles for Education:
77e8bce5-aa9e-47d3-ad59-5bd4bb13e5ac
Plan 244534:
Yearly: False
Owner: 1018324671
Price 478.15 -> 478.15
如果我只是打印原始值(删除打印中的任何舍入或浮点),那么我会弄清楚为什么它会打印出看起来相同的值:
Addons Markup:
Intune Extra Storage:
ced5f693-2d40-40ae-8848-9809ab1b0ee9
Plan 34285:
Yearly: False
Owner: 1018324671
Price 1.83000000 -> 1.8252000000000002
我的期望是:
Addons Markup:
Intune Extra Storage:
ced5f693-2d40-40ae-8848-9809ab1b0ee9
Plan 34285:
Yearly: False
Owner: 1018324671
Price 1.83 -> 1.825
答案 0 :(得分:1)
这里有两个不同的概念。 值和显示精度。如果要进行计算(尤其是在浮点数上),则应始终根据实际值进行计算,而不是“显示时的样子”。
使用浮点数(通常是货币值)的最佳方法是使用decimal
module。
这是一个简单的例子:
import decimal
i = decimal.Decimal('1.83000000')
q = decimal.Decimal('1.8252000000000002')
z = i.quantize(decimal.Decimal('0.01'), rounding=decimal.ROUND_DOWN) # get a new rounded down value
v = q.quantize(decimal.Decimal('0.01'), rounding=decimal.ROUND_DOWN)
if z > v:
print(f'{i:.3f} -> {q:.3f}') # use string formatting to control how its printed
输出为:
1.830 -> 1.825
这使计算变得更加简单,而无需处理math.isclose()
,您可以使用decimal
进行“常规数学”,它将按预期工作。