Python百分比计算0E14作为结果

2024-04-23 09:07:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的百分比计算总是打印为结果:0E-14而不是0.000037488

代码:

import decimal

firstvalue = 3.0233  # (%)
amount = 0.00310000
firstcal = decimal.Decimal(firstvalue)
firstcalresult = (firstcal / 100) * amount 

Result is 0.00009372230000, it works fine

percentage = 40

feepercent = firstcalresult
feepercentresult = (percentage / 100) * feepercent

百分比=0.00009372230000

Result is 0E-14, but must be 0.000037488

为什么第一次计算有效而第二次计算无效?我也尝试过decimal.Decimal,但结果相同。有什么想法吗?你知道吗


Tags: 代码importisitresultamount百分比decimal
2条回答

以下是没有任何错误的完整解决方案:

import decimal

firstvalue = 3.0233  # (%)
amount = 0.00310000
firstcal = decimal.Decimal(firstvalue)
firstcalresult = (firstcal / 100) * decimal.Decimal(amount)
percentage = 40
feepercent = firstcalresult
feepercentresult = decimal.Decimal(percentage / 100) * feepercent
print(decimal.Decimal(feepercentresult))

您得到的解决方案是:

0.00003748891999999999923054522233

已解决:

percentage = decimal.Decimal(40.0000)

给我正确的结果。你知道吗

相关问题 更多 >