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


Groups > comp.lang.python > #76902

Re: Working with decimals

From Seymore4Head <Seymore4Head@Hotmail.invalid>
Newsgroups comp.lang.python
Subject Re: Working with decimals
Date 2014-08-23 17:13 -0400
Message-ID <ek0iv9diq2io1126te9j5pppoilstfuvdo@4ax.com> (permalink)
References <vrihv9l5sce3bkreceav5uhkaqdo9dqnri@4ax.com>
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> wrote:

>I am trying to do this example:
>http://openbookproject.net/pybiblio/practice/wilson/loan.php
>The instructions warn that floating point math can get messy so I
>cheated a little bit to get me going.
>
>I made my program work by using numbers that wouldn't get messy.
>Instead of using 6% interest I used 10 and instead of using 12 months,
>I used 10.
>
>I managed to get it working and formatted just like they wanted it,
>but now I want to try to use any numbers.  It has been hard to figure
>out which method to use.
>
>Here is the working program.
>
>import sys
>count = 0
>payment = 0
>borrowed = 100
>rate = 10
>term = 10
>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 (repr(count).rjust(3), repr(payment).rjust(13),
>repr(balance).rjust(14))
>
>
>    payment = (borrowed + interest)/term
>    balance = balance - payment
>    count = count + 1
>
>What should I use to make the formatting come out correctly when the
>program prints "payment" and "balance" using decimal format?
>
>If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
>the screen gets very messy.
>
>Anyone care to suggest what method to use to fix the decimal format?

OK  I found the answer to my own question after getting search tips
here.

I found this function that I will be saving for later.
def make_it_money(number):
    import math
    return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

(I still need more practice to find out how it does what it does, but
I like the end result)

So I changed the line in question to:
 print (repr(count).rjust(3), make_it_money(payment).rjust(13),
make_it_money(balance).rjust(14))

So......now changing the rate from 10 to 6 and the term from 10 to 12
works fine.

I would still like to see other solutions if anyone wants to offer.

Thanks everyone

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


Thread

Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 13:47 -0400
  Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 14:21 -0400
    Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:07 -0400
      Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 15:22 -0400
      Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 20:24 +0100
        Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:48 -0400
          Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 21:31 +0100
  Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 17:13 -0400
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:47 +0100
    Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:31 +1000
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 23:47 +0100
    Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:53 +1000
    Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 00:04 -0700
      Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 10:58 -0400
      Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 11:12 -0400
        Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 14:24 -0700
          Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 19:07 -0400
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:12 +0100
    Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:17 -0600
    Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:19 -0600
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:25 +0100
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:29 +0100
      Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:16 +1000
        Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 12:27 +1000
          Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:51 +1000
            Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 13:01 +1000
    Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:37 -0600
    Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:40 -0600
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:49 +0100
  Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:52 +0100
    Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 18:03 -0400

csiph-web