Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: .format won't display my value with 2 decimal places: Why? Date: Sun, 24 Jan 2016 21:20:03 +0000 Lines: 71 Message-ID: Reply-To: MRAB Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de WvPxk/i5dQoxCnH+k5Sd0Qz0QXyndZOzeUgV6xC8DuqA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'builtin': 0.07; 'method,': 0.07; 'format:': 0.09; 'here?': 0.09; 'percentage': 0.09; 'subject:Why': 0.09; 'example:': 0.10; 'python': 0.10; 'python.': 0.11; '"michael': 0.16; '>to': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'nearest': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:{ 20': 0.18; 'string,': 0.18; '>>>': 0.20; '4.0': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'entered': 0.27; 'function': 0.28; 'values': 0.28; 'skip:( 20': 0.28; 'accepts': 0.29; 'decimal': 0.29; 'prints': 0.29; 'program,': 0.29; "i'm": 0.30; 'code': 0.30; 'class.': 0.30; 'skip:. 10': 0.32; 'run': 0.33; 'file': 0.34; 'skip:> 10': 0.35; 'item': 0.35; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'method': 0.37; 'pdf': 0.37; 'doing': 0.38; 'wrong': 0.38; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'header:Message- Id:1': 0.61; 'percent': 0.66; 'results': 0.66; 'header:Reply- To:1': 0.67; 'price': 0.69; 'discount': 0.81; '>function': 0.84; 'calculations': 0.84; 'confusing': 0.84; 'examples.': 0.84; 'percentage,': 0.84; 'programs:': 0.84; 'subject:value': 0.84; 'thing,': 0.93; 'hand,': 0.97 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=1Hv0dL8yZ9RxG2cVuH0A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 In-Reply-To: <56A1B69F.3040300@gmail.com> User-Agent: eM_Client/6.0.24432.0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102083 On 2016-01-22 04:57:03, "Michael Sullivan" wrote: >Hi. I'm very very new to python. I have been working my way through a= =20 >free python pdf file I found (python3handson.pdf) and I'm having=20 >trouble with one of my programs: > > >'''discount.py > >Exercise 1.14.3.1. * Write a program, discount.py, that prompts the=20 >user for an original price and >for a discount percentage and prints out the new price to the nearest=20 >cent. For example if the user enters >2.89 for the price and 20 for the discount percentage, the value would=20 >be (1- 20/100)*2.89, rounded to two >decimal places, 2.31. For price .65 with a 25 percent discount, the=20 >value would be (1- 25/100)*.65, rounded >to two decimal places, .49. 10 Write the general calculation code=20 >following the pattern of the calculations >illustrated in the two concrete examples. > >''' > >oPrice =3D 0 >newPrice =3D 0 >discount =3D 0 > >oPrice =3D input('What is the original price? ') >discount =3D input('How much is this item discounted? ') > >oPrice =3D float(oPrice) >discount =3D float(discount) >newPrice =3D 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=20 >the discount, it always results in 4.0. When I entered my format=20 >function call directly into the shell, it comes out like I would=20 >expect: > > > > >>> format(4.0, '.2f') >'4.00' > > >What exactly am I doing wrong here? > You're confusing the builtin 'format' function with the 'format' method=20 of the 'str' class. The format function accepts a value and a format: >>> format(4.0, '.2f') '4.00' The format method, on the other hand, belongs to the format string it's=20 attached to. In this example: 'The new price is {}' .format(newPrice, '.2f') the format string is 'The new price is {}' and you're calling its=20 'format' method with 2 values for that string, the first being 4.0=20 (used) and the second on being '.2f' (unused). What you want is: print('The new price is {:.2f}'.format(newPrice))