"Christopher Spears" <[EMAIL PROTECTED]> wrote

> def calculate_price(price, percent_tax):
>    sales_tax = price * percent_tax
>    new_price = price + sales_tax
>    return new_price
> 
> price = float(raw_input("Enter a price: "))
> percent_tax = float(raw_input("Enter a sales tax: "))
> print "%.2f" % calculate_price(price, percent_tax)
> 
> Here is the script in action:
> [EMAIL PROTECTED] ./chap5 173> python sales_tax.py
> Enter a price: 10.00
> Enter a sales tax: .0825
> 10.82
> 
> I'm not convinced that the new price is completely
> accurate because the price without the string
> formating is 10.825

Of course, its just doing a multiply/divide.

> Wouldn't the price be rounded up to 10.83 in the real
> world?  How can I accomplish this?

Depends on the tax laws in your neck of the woods. 
In the UK you are not allowed to charge more than the tax 
so it would be rounded *down* to 10.82.

But you should never use floats for financial calculations 
because they are inherently inaccurate. Its usually better 
to multiply sums up to integer values of pennies and then 
convert back to decimal at the point of display. Either that 
or use a BCD type module such as Python's decimal module.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to