Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102081 > unrolled thread

.format won't display my value with 2 decimal places: Why?

Started byMichael Sullivan <msulli1355@gmail.com>
First post2016-01-21 22:57 -0600
Last post2016-01-21 22:57 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  .format won't display my value with 2 decimal places: Why? Michael Sullivan <msulli1355@gmail.com> - 2016-01-21 22:57 -0600

#102081 — .format won't display my value with 2 decimal places: Why?

FromMichael Sullivan <msulli1355@gmail.com>
Date2016-01-21 22:57 -0600
Subject.format won't display my value with 2 decimal places: Why?
Message-ID<mailman.204.1453668644.15297.python-list@python.org>
Hi.  I'm very very new to python.  I have been working my way through a 
free python pdf file I found (python3handson.pdf) and I'm having trouble 
with one of my programs:


'''discount.py

Exercise 1.14.3.1. * Write a program, discount.py, that prompts the user 
for an original price and
for a discount percentage and prints out the new price to the nearest 
cent. For example if the user enters
2.89 for the price and 20 for the discount percentage, the value would 
be (1- 20/100)*2.89, rounded to two
decimal places, 2.31. For price .65 with a 25 percent discount, the 
value would be (1- 25/100)*.65, rounded
to two decimal places, .49. 10 Write the general calculation code 
following the pattern of the calculations
illustrated in the two concrete examples.

'''

oPrice = 0
newPrice = 0
discount = 0

oPrice = input('What is the original price?  ')
discount = input('How much is this item discounted?  ')

oPrice = float(oPrice)
discount = float(discount)
newPrice = oPrice - (oPrice * discount)

print('The new price is {}' .format(newPrice, '.2f'))


When I run this thing, and I enter 5.00 for original price and .2 for 
the discount, it always results in 4.0.  When I entered my format 
function call directly into the shell, it comes out like I would expect:



 >>> format(4.0, '.2f')
'4.00'


What exactly am I doing wrong here?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web