如何在Python中返回正确舍入的答案?

时间:2014-11-10 17:55:07

标签: python rounding

我正在编写一个脚本,要求用户提供他们拥有的不同面额的硬币数量,然后以美元和美分显示总金额。

然而,当它显示美元时,它会得出答案。

例如,如果总计是82美分,它表示我有1美元,而我需要它说0美元。

我无法在此适当地使用 round 功能。

使用它是否是明智之举,还是应该坚持使用 format 功能?

def main():
    pennies  = get_input( "Enter pennies  : " )
    nickels  = get_input( "Enter nickels  : " )
    dimes    = get_input( "Enter dimes    : " )
    quarters = get_input( "Enter quarters : " )

    print( "You Entered : " )
    print( "\tPennies   : " , pennies )
    print( "\tNickels   : " , nickels )
    print( "\tDimes     : " , dimes )
    print( "\tQuarters  : " , quarters )

    total_value     = get_total(           pennies, nickels, dimes, quarters )
    dollars         = get_dollars(         pennies, nickels, dimes, quarters )
    left_over_cents = get_left_over_cents( pennies, nickels, dimes, quarters )

    print( "Total = $",format( total_value, ".2f" ), sep = "" )
    print( "You Have", format( dollars, ".0f" ), "dollars and", left_over_cents, "cent(s)" )

def get_input( message ):
    notify = int( input( "I don't know" ) )
    while notify >= 0:
        return notify

def get_total( pennies, nickels, dimes, quarters ):
    pennies         = .01 * pennies;
    nickels         = .05 * nickels;
    dimes           = .10 * dimes;
    quarters        = .25 * quarters;
    total_value     = pennies + nickels + dimes + quarters
    return total_value

def get_dollars( pennies, nickels, dimes, quarters ):
    pennies         = .01 * pennies;
    nickels         = .05 * nickels;
    dimes           = .10 * dimes;
    quarters        = .25 * quarters;
    total_value     = pennies + nickels + dimes + quarters
    dollars         = total_value
    return dollars

def get_left_over_cents( pennies, nickels, dimes, quarters ):
    pennies         = .01 * pennies;
    nickels         = .05 * nickels;
    dimes           = .10 * dimes;
    quarters        = .25 * quarters;
    total_value     = pennies + nickels + dimes + quarters
    left_over_cents = total_value
    return left_over_cents

main()

1 个答案:

答案 0 :(得分:2)

通常情况下,最好使用decimal.Decimal作为货币值:

import decimal
penny_value = decimal.Decimal('.01')
nickle_value = decimal.Decimal('.05')
dime_value = decimal.Decimal('.10')
quarter_value = decimal.Decimal('.25')

如果您要保留硬币数量,您应该将上述常数乘以您的硬币数量,并且您不会有舍入错误。

pennies = 2
nickles = 1
dimes = 0
quarters = 3

total_value = (pennies * penny_value + nickles * nickle_value + 
               dimes * dime_value + quarters * quarter_value)
dollars = total_value.quantize(1, rounding=decimal.ROUND_DOWN)

dollars现在是Decimal('0'),等于零。

>>> print total_value
0.82
>>> print dollars
0

如果你想坚持使用int和float来向下舍入,转换为int,它将向零舍入:

total_value = .82

dollar_value = int(total_value)

dollar_value现在是0

要使用此方法,请执行以下操作:

def get_dollars(pennies, nickels, dimes, quarters):
    pennies = .01 * pennies;
    nickels = .05 * nickels;
    dimes = .10 * dimes;
    quarters = .25 * quarters;
    total_value= pennies + nickels + dimes + quarters
    dollars = int(total_value)
    return dollars