【问题标题】:Rounding in PythonPython中的四舍五入
【发布时间】:2013-12-11 15:57:21
【问题描述】:

大家好,我目前正在做一个学校项目,甚至我的老师也很难过。在加拿大,一分钱已被删除,因此现在所有购买都四舍五入为 0 或 5。例如,5.53 将变为 5.55,5.52 将变为 5.50。我试图让我的程序像这样循环,但我不知道如何。我知道如何四舍五入到小数位,但我不知道如何四舍五入到这样的细节。任何帮助将不胜感激!

这是我的代码。该项目是关于制作一个收银员可以在咖啡店使用的程序。

order = ['coffee', 'tea', 'hashbrown','jelly','cream','chocolate','glazed','sandwich','bagel','cookie','pannini']
quantity = ['0','0','0','0','0','0','0','0','0','0','0']

# coffee = $1 
# Tea = $1.30 
# hashbrown = $1.25 
# all donuts = $1.50 
# sandwich = $2.50 
# bagel = $2 
# cookie = $0.50 
# pannini = $4

cashier = 1
total = 0

while cashier == 1:
    print "What did the customer order?"
    ordered = input ()

    while ordered > 10 or ordered < 0:
        print "Do you want to input a valid order?"
        ordered = input ()



    print "How many are being ordered?"
    quantityorder = input ()
    quantity[ordered] = quantityorder
    print "Ordered",quantityorder,"",order[ordered],"!"  
    if ordered == 0:
        ordered = 1.0
    elif ordered == 1:
        ordered = 1.30
    elif ordered == 2:
        ordered = 1.25
    elif ordered == 3 or ordered == 4 or ordered == 5 or ordered == 6:
        ordered = 1.50
    elif ordered == 7:
        ordered = 2.50
    elif ordered == 8:
        ordered = 2
    elif ordered == 9:
        ordered = 0.50
    else:
        ordered = 4.0


    price = ordered * quantityorder
    total = total + price
    print "Anything else?"
    cashier = input ()  #If the user inputs 1 then they can input another order if they didn't put in 1 then the program assumes that it is the end of a customers order



print "Your total is $", total * 1.13,"!"
total = total * 1.13
print
print "How much money was given?"
print
money = input ()* 1.0
while money < total:
    print "Please input a valid number!"
    money = input ()

print "The change should be $",money - total,"!"

【问题讨论】:

  • 为我们提供一些代码。请记住,您的程序可能将数字处理为 numbersstrings,并且处理它的最佳方法是不同的,具体取决于程序会处理您的号码。给我们代码,你给我们上下文来帮助我们给你一个有意义的答案!
  • 将其翻倍,圆形,然后减半。
  • 目前投票率最高的答案(@haael,依靠quantize)是完全错误的。我目前没有足够的时间来真正制定出一个强大的解决方案,但是来自相关问题的this answer 是一种更好的方法。是的,它是float,这是我必须检查的,但如果需要,至少它可以适应Decimal
  • 另外,对于它的价值,@wim 的评论包含一种可行的方法。它可以充实为一个答案。

标签: python decimal rounding


【解决方案1】:

这一直折磨着我,直到我解决了它。我为自己设定的一条规则是不要在模 5 上使用个案切换,而是使用内置函数。不错的拼图。 wim 的“双倍,圆,然后减半”评论很接近。

def nickelround(n):
    N = int(n)
    print "%0.2f" % (N + round((n - N) * 20) * 0.05)

【讨论】:

    【解决方案2】:
    In [42]:
    x=0.57
    int(x/0.05)*0.05
    
    Out[42]:
    0.55
    

    【讨论】:

      【解决方案3】:

      通常在这些编程问题中,您会被明确要求解决如何“做出改变”,而不仅仅是提供到期的改变总量(这是微不足道的)。因此,您可以将便士校正内联到该函数中:

      def make_change(bal):
          bal = bal + .02 #correction for no pennies
          currency = [20,10,5,1,.25,.1,.05]
          change = {}
          for unit in currency:
              change[unit] = int(bal // unit)
              bal %= unit
          return change
      

      这个特殊的表格返回一个dict的找零面额及其对应的计数。

      【讨论】:

        【解决方案4】:

        正如您已经暗示的那样,使用 Decimal 类(模块十进制)。用 Decimal('13.52') 等十进制构造函数替换代码中的所有浮点数。

        现在您要使用的功能是“量化”。

        你这样使用它:

        Decimal('1.63').quantize(Decimal('0.5'), rounding=ROUND_HALF_DOWN)
        

        参数 Decimal('0.5') 表示要四舍五入。

        更新: 由于 OP 想要四舍五入到 0.05 的单位,他显然必须使用:

        Decimal('1.63').quantize(Decimal('0.05'), rounding=ROUND_HALF_DOWN)
        

        【讨论】:

        • 是的,如果它是 1.63,我希望它舍入到 1.65
        • 然后使用 quantize(Decimal('0.05'))。
        • 您如何从中获得 1.65?当我尝试它时,它似乎保持在 1.63。
        • 这是一个完全错误的答案。它完全错过了quantize 的工作方式,并且没有按照问题的要求进行操作。令人费解的是,它是如何获得任何支持的,更不用说 4 了。
        【解决方案5】:

        我将老派说,将您的数字转换为带有 null 或某种标记以表示小数的值列表。

        创建一个变量来保存您的四舍五入数字并将每个值从 1000 或其他任何值添加到 100、10 和 1。 (你当然是在循环)。

        一旦你的循环命中标签,取决于你想要四舍五入的距离,(这应该是你的函数的一个参数)抛出一个条件来表示你想要如何四舍五入,即。 5 表示向上取整,4 向下取整(这是另一个参数)。然后添加最终值并返回新舍入的数字。

        第 2 步:解雇你的老师,因为这是一个难以置信的简单问题。

        请记住,任何了解 c 或使用过内存分配的人都应该轻而易举地发现这个问题。

        【讨论】:

          【解决方案6】:

          您可以使用这样简单的方法将您的数字四舍五入到给定的基数:

          def round_func(x, base=0.05):
              return round(base*round(float(x)/base), 2)
          

          由于您只需要 2 位小数,您可以将其四舍五入到 2 位小数。内轮函数是根据基数对给定数字进行四舍五入,在您的情况下为 0.05。

          【讨论】:

            【解决方案7】:

            你知道如何使用自己的函数吗? 将此函数添加到您的代码中:

            def nickelround(m):
              return round(m/0.05,0)*0.05
            

            并使用它:

            print "The change should be $", nickelround(money - total),"!" 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2020-03-05
              • 2014-05-05
              • 1970-01-01
              • 2023-01-12
              • 2018-09-14
              • 2023-03-22
              • 2012-08-04
              相关资源
              最近更新 更多