Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

(Golf) Formatting NumberTo Money

by enoch (Chaplain)
on Aug 29, 2002 at 16:33 UTC ( #193816=perlmeditation: print w/replies, xml ) Need Help??

It has been a while since there has been a good round of golf, so I decided to post one. I couldn't come up with a nice interesting math brain-teaser, so I'll post this one.

The Rules
  • Write a sub that takes a number as an argument and returns it in the standard monetary format -- with comma's and to two decimal places

Expected Input-Output
  • Input: 3
  • Output: 3.00

  • Input: 1000
  • Output: 1,000.00

  • Input: 12345678
  • Output: 12,345,678.00

  • Input: 4444444.11
  • Output: 4,444,444.11

  • Input: 98765.343
  • Output: 98,765.34

  • Input: 111.229
  • Output: 111.23

My Swing (83 chars)
sub a { $_=pop;($_=reverse sprintf"%.2f",$_)=~s/(\d\d\d(?=\d))/$1,/g,return re +verse split'' }

Enoch

(update: Changed "integer" to "number". Integers don't have decimals. Thanks to xylus for pointing out my err.

Replies are listed 'Best First'.
Re: (Golf) Formatting Integer To Money
by Abigail-II (Bishop) on Aug 29, 2002 at 17:04 UTC
    57 characters:
    sub a{$_=sprintf"%.2f",pop;{s/(\d)(\d{3})(?!\d)/$1,$2/&&redo}$_}
    Abigail
      49 Characters:
      sub a{$_=sprintf"%.2f",pop;while(s/\B\d{3}\b/,$&/){}$_}
      Update: 48 characters:
      sub a{$_=sprintf"%.2f",pop;{s/\B\d{3}\b/,$&/&&redo}$_}
      --Dave
        Here's a 44 with a few small improvements to yours:
        sub a{$_=sprintf"%.2f",@_;1while s/\B...\D/,$&/;$_}
        It seems that the sprintf solutions fail sometimes in rounding, e.g. "123456789012345.125" becomes "123,456,789,012,345.12" instead of "123,456,789,012,345.13".

        Update: I was just informed by thospel++ that IEEE rounding is toward even, not upward. That explains the behavior, though I'm not sure if it's the desired behavior for this problem.

        -- Mike

        --
        just,my${.02}

      Why the need for sprintf ?? Am I missing something of importance?

      If sprintf is not needed, here is one at 49 chars....
      sub a{$_=pop;{s/(\d)(\d{3})(?!\d)/$1,$2/&&redo}$_.'.00'}
      update: I am a little confused about the input, the post asks for an integer, which would not have any decimal places, but some of the input has a decimal place..
      Should this handle input with decimal places, or should it just trust that the user submits integers only?
      perl -e '$_=$0;split??;chop$_;$;=pop@_;$;++for(0..9060420);$_.=reverse +$;;print'
        To get the precision correct. Your script fails if you input 1234.56.

        ++Abigail-II

        enoch
Re: (Golf) Formatting NumberTo Money
by Anonymous Monk on Aug 29, 2002 at 18:36 UTC
    # 48 sub f { use Number::Format':subs';format_number(pop,2,2) } #23456789012345678901234567890123456789012345678
Re: (Golf) Formatting Integer To Money
by Aristotle (Chancellor) on Aug 29, 2002 at 17:53 UTC
    53
    # 1 2 3 4 5 #2345678901234567890123456789012345678901234567890123 $_=sprintf"%.2f",pop;{s/(^\d+)(\d{3})/$1,$2/&&redo}$_

    Makeshifts last the longest.

Re: (Golf) Formatting NumberTo Money
by Django (Pilgrim) on Aug 29, 2002 at 19:06 UTC
    54 without declaration, else 61
    sub a{$_=sprintf"%.2f",pop;{s/(\d+)(\d{3}.*)/$1,$2/?redo:$_}}

    ~Django

    "Why don't we ever challenge the spherical earth theory?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://193816]
Approved by TStanley
help
Chatterbox?
[Corion]: :-D
Corion discovers a new shiny toy to try out over the (longish) weekend. Since I've done some more with websockets, maybe I'll try writing a webserver that implements hot-reloading of HTML(+CSS, +Javascript) in the browser. Edit the local file and ...
[Corion]: ... the browser(s) get a ping to a) refresh the page or b) reload "just" the changed parts, keeping the scroll position etc.
[Corion]: But I also have to look at how I can make WWW::Mechanize:: RemoteBrowser a reality, and how to make it safe from malicious content ;)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-04-26 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?