Ian Witham wrote:
> As Michael points out, you need to explicitly use the round function, as
> the float formatting merely truncates anything after the second decimal
> place.
No. I don't know what algorithm it uses for rounding, but it does round:
In [3]: '%.2f' % 6.
Out[3]: '6.67'
In [4]:
-Original Message-
From: Ian Witham <[EMAIL PROTECTED]>
Sent: Sep 18, 2007 11:39 PM
To: Christopher Spears <[EMAIL PROTECTED]>
Cc: tutor@python.org
Subject: Re: [Tutor] sales tax
As Michael points out, you need to explicitly use the round function, as the float formatting mere
"Michael Langford" <[EMAIL PROTECTED]> wrote
> PS: The function you're looking for is called round. Its first param
> is the
> number to round, the seconds is how many digits past the radix point
> to
> keep.
Caveat:
Note that round will perform math style rounding - sometimes
rounding up, some
"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 "
There is definitely a secondary understanding of math that comes from
working on computers. How computers do math is quite a bit different from
how you or I do it on a piece of paper. The fact that python can do
arbitrary large integer arithmetic makes it quite nice for many math
applications as yo
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
This function can easily found using the google programming rule:
I want a function that does 'action'
Type: into google
Look in top 5 results. If that doesn't work, try synonyms for 'action'
--Michael
PS: The function you're looking for is called round. Its first param is the
number to
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 = fl