【问题标题】:Rounding, variable from a .txt in Python 3 and extra舍入,来自 Python 3 中 .txt 的变量等
【发布时间】:2016-12-18 13:03:46
【问题描述】:
if op in ["/"]:
    # rounding does not rounds half up so I imported a module
    from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_DOWN
    Decimal(str(answer)).quantize(Decimal("1.11"), rounding=ROUND_HALF_UP)
    Decimal(str(given_answer)).quantize(Decimal("1.11"), rounding=ROUND_HALF_UP)
print("correct {}".format(answer, ".2f"))

49//34=?1.44

正确 1

回答错误!

如果真正的答案是 3.55865465,我希望它的正确答案是 3.56。我做错了什么?

【问题讨论】:

  • 49/34 是 1.44 为什么你希望它是 3.56?正确的答案是什么?
  • 我并不是说我想要 49/34 的 3.56。 “如果真正的答案是 3.55865465,我希望它的正确答案是 3.56。”

标签: python python-3.x counter division rounding-error


【解决方案1】:

尝试将最后一行更改为以下内容:

print("correct {:.2f}".format(answer))

【讨论】:

  • 实际值为 2,777777777778,我希望将实数四舍五入为 2.78 有没有办法做到这一点?感谢另一部分。
  • 是的,您需要指定您的输入是浮点数。例如: answer = float(100) / 36 print("正确 {:.2f}".format(answer))
猜你喜欢
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2023-03-24
  • 2020-03-05
相关资源
最近更新 更多