Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'broken': 0.04; 'from:addr:yahoo.co.uk': 0.04; 'error:': 0.07; 'reason,': 0.07; 'sys': 0.07; 'string': 0.09; 'formatting': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'language.': 0.14; 'mostly': 0.14; 'btw': 0.16; 'count,': 0.16; 'formatting.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'appropriate': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'pointed': 0.19; 'working.': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'adds': 0.24; 'skip:{ 20': 0.24; 'math': 0.24; 'options': 0.25; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'points': 0.29; "doesn't": 0.30; 'skip:( 20': 0.30; 'decimal': 0.31; 'another': 0.32; 'up.': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'in.': 0.36; 'error.': 0.37; 'to:addr:python- list': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'up,': 0.60; 'most': 0.60; 'balance': 0.61; 'you.': 0.62; 'back': 0.62; "you'll": 0.62; 'term': 0.63; 'myself': 0.63; 'our': 0.64; 'interest': 0.64; 'charset:windows-1252': 0.65; 'lose': 0.68; 'limit': 0.70; 'url:i': 0.72; '100': 0.79; 'trial': 0.83; 'url:jpg': 0.83; 'digits?': 0.84; 'kiss': 0.84; 'url:imgur': 0.84; '000': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Working with decimals part 2 Date: Mon, 25 Aug 2014 19:23:36 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-187-85.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408991038 news.xs4all.nl 2885 [2001:888:2000:d::a6]:55553 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76996 On 25/08/2014 18:55, Seymore4Head wrote: > import sys > import math > def row1(number): > return str(number).rjust(3) > def row2(number): > return str(format(number) ',.2f')) > def row3(number): > return '${:.2f}'.format(number) > def row4(number): > return '$' + str(format(math.floor(number * 100) / 100, ',.2f')) > > count = 0 > payment = 0 > borrowed = 100 > rate = 6 > term = 12 > interest=borrowed*rate*.01 #(*1) > balance = borrowed + interest > print ("Loan calculator") > print ("") > print ("Amount borrowed: ", borrowed) > print ("Interest rate: ", rate) > print ("Term: (months)", term) > print ("") > print ("Amount borrowed:" , borrowed) > print ("Total interest paid:" , interest) > print ("") > print ("") > print (" Amount Remaining") > print ("Pymt# Paid Balance") > print ("----- ------ ----------") > while count <=term: > > print ("{} {} {}".format(row1(count), > row2(payment),row3(balance))) > > payment = (borrowed + interest)/term > balance = balance - payment > count = count + 1 > > > I changed the program just a little to give myself a little practice > with number formats. The main thing I wanted to do was make the > decimal points line up. The problem I am having is with the print > (count)(payment)(balance) line. > > I added 4 functions row1-4 for some practice in formatting. > Row4 is the old makeitmoney function. I am not using it, but I am > keeping it in. > > row2 is row4 with: > (math.floor(number * 100) / 100, ',.2f') > taken out leaving ',.2f' > For some reason, it is not working. If I try to use row2 I get this > error: > http://i.imgur.com/FgeF9c9.jpg > > Most of my learning is trial and error. Mostly error. To try to get > the decimals to line up, I changed row3 from'${:.2f}' to '${:6.2f}'. > That makes the decimals line up, but it adds another problem. > http://i.imgur.com/1KsP3ga.jpg > > If you change "borrowed" from 100 to 1000 the fix gets broken again. > So I changed the '${:6.2f}' to '${:8.2f}' > http://i.imgur.com/74C5sAx.jpg > > That works until you change "borrowed" to 1000000 > http://i.imgur.com/fCuwOXv.jpg > Is there a way to fix the decimal point to line up without having to > limit the whole digits? > > BTW I changed row3 back to '${:6.2f}' and used 1 000 000 000 for > "borrowed" It doesn't lose any digits in the whole number column, but > it does skew the formatting. > http://i.imgur.com/Hjpkts4.jpg > The best approach to trial and error is to use the interactive prompt. You'll need something like it if you insist on mixing function calls that contain various types of string formatting with string formatting. An alternative is to apply the KISS principle. print ("{x}{y}{z}".format(count, payment, balance)) is all you need, where x, y and z are the appropriate formatting options for each of count, payment and balance. These options have already been pointed out to you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence