As Michael points out, you need to explicitly use the round function, as the float formatting merely truncates anything after the second decimal place.
I ran across a similar problem with the int() fuction early on. Anything after the decimal point is truncated, not rounded, leading to behavior I was not expecting. For example: int(-1.5) == -1 int(-.5) == 0 int(.5) == 0 int(1.5) == 1 Ian On 9/19/07, Christopher Spears <[EMAIL PROTECTED]> wrote: > > I wrote a script that takes a price and a sales tax > and calculates the new price. > > #!/usr/bin/env python > > 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 > > [EMAIL PROTECTED] ./chap5 164> python sales_tax.py > Enter a price: 10 > Enter a sales tax: .0825 > 10.825000 > > Wouldn't the price be rounded up to 10.83 in the real > world? How can I accomplish this? > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor >
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor