[Tutor] quick question

Dwight Hutto dwightdhutto at gmail.com
Fri Sep 28 07:27:19 CEST 2012


On Fri, Sep 28, 2012 at 1:18 AM, jh <xperimental22 at gmail.com> wrote:
> Howdy Folks,
>
> I'm using Python 3.2.3, IDLE 3.2.3 (on Windows 7 64-bit if it matters)
>
> Here is my code:
>
>
> # Write a program that asks for the prices of 5 items then displays the
> subtotal
> # of the 5 items, then calculates sales tax (6 percent), and the displays
> the total.
>
> # Get input for the prices of 5 items from the user
>
> item1 = float(input('Enter the price of item 1: '))
> item2 = float(input('Enter the price of item 2: '))
> item3 = float(input('Enter the price of item 3: '))
> item4 = float(input('Enter the price of item 4: '))
> item5 = float(input('Enter the price of item 5: '))
>
> # Calculate and print the subtotal of the items
>
> subtotal = item1 + item2 + item3 + item4 + item5
>
> print('The subtotal of your items is:', subtotal)
>
> # Calculate sales tax and add it to the subtotal
>
> tax = subtotal * .06
> total = subtotal + tax
>
> # Display the total to the user
>
> print('The total amount of your items plus tax is:' , \
>       format(total, ',.2f'))
>
>
> And here's my output:
>
> Enter the price of item 1: 12.36
> Enter the price of item 2: 55.63
> Enter the price of item 3: 32.36
> Enter the price of item 4: 358.28
> Enter the price of item 5: 25552.22
> The subtotal of your items is: 26010.850000000002
> The total amount of your items plus tax is: 27,571.50
>
> My question here is, why does my subtotal have so many decimals when I never
> went above 2 in my input?
>
> Thanks in advance,
> J
>

>>> x = round(float(10),2)
>>> x
10.0
>>> x + round(float(1000.755555),2)
1010.76
>>>


You have to round(float_number_variable,2)  the float off.

-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com


More information about the Tutor mailing list