代码之家  ›  专栏  ›  技术社区  ›  Polyterpe

四舍五入,变量来自Python 3中的.txt和额外

  •  0
  • Polyterpe  · 技术社区  · 8 年前
    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。我做错了什么?

    Written text file with python

    1 回复  |  直到 8 年前
        1
  •  2
  •   Stian    8 年前

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

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