首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python折扣率2项加税程序输出不正确

问Python折扣率2项加税程序输出不正确
EN

Stack Overflow用户
提问于 2019-09-11 05:18:32
回答 1查看 50关注 0票数 0

我正在写一个程序,输入2商品,应用25%折扣,并在总折扣商品价格上添加8.25%税率,但我总是得到错误的输出。

它可以处理某些数字,例如5 + 3,但不能正确处理8.75 + 25.99。

代码语言:javascript
复制
first_item = int(float(input("Price of 1st item: ")))

second_item = int(float(input("Price of 2nd item: ")))

discount = (first_item + second_item) *.25

discounted_items = (first_item + second_item) - discount

tax = discounted_items *.0825

final_pay = discounted_items + tax

print("You pay ", final_pay)

8.75 + 25.99的预期结果是28.204537,但我一直得到26.7918。

EN

回答 1

Stack Overflow用户

发布于 2019-09-11 05:23:51

将输入从str转换为float很好,但当您再次将其转换为int时,会丢失小数部分(.xxx),因此会丢失精度。3和5没有出现此问题,因为它们已经是整数,因此不会丢失任何内容,因此只需保留输入float即可

代码语言:javascript
复制
first_item = float(input("Price of 1st item: "))
second_item = float(input("Price of 2nd item: "))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57878325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档