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


Groups > comp.lang.python > #76931

Re: Working with decimals

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

Show all headers | View raw


On Sun, 24 Aug 2014 00:04:29 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:

>On 08/23/2014 02:13 PM, Seymore4Head wrote:
>> On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
>>
>> 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)
>
>That's total nonsense and overkill!  If you really want to do it with a separate function, using 
>old style:
>
>def make_it_money(number):
>     return '$%.2f' % number
>
>or using new style:
>
>def make_it_money(number):
>     return '${:.2f}'.format(number)
>
>But even these functions are unnecessary.  Use either of these formatting methods directly in 
>the print() statement...
>
>>
>> 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))
>
>print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
>
>or
>
>print('%3d $%-13.2f $%-14.2f' % (count, payment, balance))
>
>But please, please, PLEASE first go through a real tutorial, and WORK the examples to fix them 
>in your mind.  Questions like these will all be covered there.  And you'll learn the language as 
>a whole instead of trying to be spoon-fed isolated answers.  It will be well worth your time.
>
>The tutorial on the official Python web site is a good one (of course there are many others)
>
>docs.python.org/3/tutorial/index.html
>
>It does appear that you're using Py3, but in case you're using Py2, change the '3' in that URL 
>to '2'.
>
>(Print formatting is in section 7)
>
>      -=- Larry -=-
>
>PS.  Oops, my bad...  I just double checked my suggestions, which left-justified the values, but 
>I see you want them right-justified (which keeps the decimal points lined up).  This complicates 
>it a bit to keep the dollar-sign butted up against the value, and it makes it necessary to use 
>that make_it_money() function I said was unnecessary.  But it's still unnecessary by using a 
>little different finagling...  Try either of these versions:
>
>print('{:3d} {:>13s} {:>14s}'.format(count,
>         '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
>print('%3d %13s %14s' % (count, '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
Thanks for sharing these.  I tried every single one.
The first two you gave didn't format correctly (as you noted) but it
seems like they should have.
If I understand this one:
>print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
the ${:<13.2f} part would have lined up correctly if the number had 13
digits before the decimal.  The leading 0's were ignored.  

The last two hit the spot when the last digit is not a 0.  When the
last digit is a 0, it causes the decimals not to line up.

Tiny quirks like that can really be frustrating if you are trying to
deliver a polished product.  Luck for me, I am not.

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