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


Groups > comp.lang.python > #76997

Re: Working with decimals part 2

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'broken': 0.04; 'string.': 0.05; 'error:': 0.07; 'reason,': 0.07; 'sys': 0.07; 'steps:': 0.09; 'def': 0.12; 'mostly': 0.14; 'btw': 0.16; 'formatting.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'working.': 0.19; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'adds': 0.24; 'skip:{ 20': 0.24; 'string,': 0.24; 'math': 0.24; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'points': 0.29; "doesn't": 0.30; 'skip:( 20': 0.30; 'decimal': 0.31; 'another': 0.32; 'up.': 0.33; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'problem.': 0.35; 'but': 0.35; 'there': 0.35; 'in.': 0.36; 'done': 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; 'up,': 0.60; 'most': 0.60; 'balance': 0.61; 'back': 0.62; 'term': 0.63; 'myself': 0.63; 'interest': 0.64; 'lose': 0.68; 'limit': 0.70; 'url:i': 0.72; '100': 0.79; 'trial': 0.83; 'url:jpg': 0.83; 'digits?': 0.84; 'url:imgur': 0.84; '000': 0.93
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=SYl5d5hu c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=WfxhgCanec8A:10 a=GsQ4dI1Gs44A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=6dZFNA6OAAAA:20 a=KtQdoxfuAAAA:20 a=vYo_BMAGAAAA:20 a=oxb0NnO0AAAA:20 a=Lu0gqhCLAAAA:20 a=cc0D7mIWEkG_brSyFKAA:9 a=ppacoZ5ns1TzM4N1:21 a=J-Lr72-WuQekAblE:21 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Mon, 25 Aug 2014 19:26:39 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Working with decimals part 2
References <hsomv9hvf31ntf45rr2ukhb0078cubovre@4ax.com>
In-Reply-To <hsomv9hvf31ntf45rr2ukhb0078cubovre@4ax.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13427.1408991207.18130.python-list@python.org> (permalink)
Lines 92
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408991207 news.xs4all.nl 2913 [2001:888:2000:d::a6]:58206
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76997

Show key headers only | View raw


On 2014-08-25 18:55, Seymore4Head wrote:
> import sys
> import math
> def row1(number):
>      return str(number).rjust(3)
> def row2(number):
>      return str(format(number) ',.2f'))

That line has to many ')'.

The result of 'format' is a string, so there's no need to use 'str'.

> def row3(number):
>      return '${:.2f}'.format(number)
> def row4(number):
>      return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

Here, again, the result of 'format' is a string, so there's no need to 
use 'str'.
>
> 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
>
There are 2 steps:

1. Format the amount as a string. This is best done in a function.

2. Right-justify the string. This could be done as part of the format
for the row.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Working with decimals part 2 Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 13:55 -0400
  Re: Working with decimals part 2 Chris Angelico <rosuav@gmail.com> - 2014-08-26 04:16 +1000
  Re: Working with decimals part 2 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-25 19:23 +0100
  Re: Working with decimals part 2 MRAB <python@mrabarnett.plus.com> - 2014-08-25 19:26 +0100
  Re: Working with decimals part 2 alex23 <wuwei23@gmail.com> - 2014-08-26 10:46 +1000
    Re: Working with decimals part 2 Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-27 19:55 -0400

csiph-web