Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Making money look nice

by CodeJunkie (Monk)
on Apr 11, 2003 at 23:35 UTC ( [id://249997]=perlquestion: print w/replies, xml ) Need Help??

CodeJunkie has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
Can anyone help me make my money look nice!?

I have a value of, say £195000. This is ugly looking and I want to put commas in it to make it more readable. I.e. £195,000.

Now I can do this fine if I know how long the number is, i.e. do some kind of substitution based on \d{3} but what if I don't know how long the number is?

I guess I would have to count the number of digits to work out the number of commas, then count in 3's from the right, adding commas where appropriate.

Anyway, I thought maybe someone could just tell me there is a cool little module that does this for me, or perhaps give me a clue what the substitution would look like?

Thanks for any help,
Cheers,
Tom

Replies are listed 'Best First'.
Re: Making money look nice
by valdez (Monsignor) on Apr 12, 2003 at 10:00 UTC

    I used Math::Currency:

    use Math::Currency qw(Money $LC_MONETARY); Math::Currency->format( $LC_MONETARY->{EUR} ); Math::Currency->format('MON_THOUSANDS_SEP', '\''); print Math::Currency::Money(12345.67);

    this code prints €12'345,67

    HTH, Valerio

Re: Making money look nice
by The Mad Hatter (Priest) on Apr 12, 2003 at 00:15 UTC
    And yet another way, with the sub straight from The Perl Cookbook...
    while (<DATA>) { chomp; print commify($_), "\n"; } # From _The Perl Cookbook_, recipe 2.17 sub commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } __DATA__ 195000 42000 2100
    This outputs
    195,000 42,000 2,100
    Here's the discussion in the book:
    It's a lot easier in regular expressions to work from the front than from the back. With this in mind, we reverse the string and make a minor change to the algorithm that repeatedly inserts commas three digits from the end. When all insertions are done, we reverse the final string and return it. Because reverse is sensitive to its implicit return context, we force it to scalar context.

      For a good time read about sexegers.


      TGI says moo

Re: Making money look nice
by derby (Abbot) on Apr 11, 2003 at 23:42 UTC
    perldoc -q comma

    -derby

      From "Mastering Regular Expressions 2nd Edn" p.65

      s/(?<=\d)(?=(\d\d\d)+$)/,/g

      I like this because you are actually using the regex to determine the location to insert the comma, as opposed to replacing any of the original string. HTH

      derby,
      The only thing I would like to add is from the Perl FAQ. If you want to print money, then be sure to strip off the leading '$' (or appropriate symbol) before calling commify().

      Cheers - L~R

Re: Making money look nice
by pg (Canon) on Apr 12, 2003 at 01:35 UTC
    I think that there should be a module under Locale::, so I briefly checked those Locale:: modules both in the core and CPAN, seems to me that no existing Locale:: module handles this.

    But I think Locale:: is the best place to hold this kind of functionality, and it could be called Locale::NumberFormat.

Re: Making money look nice
by benn (Vicar) on Apr 12, 2003 at 11:31 UTC
    Here's my home-brewed 'PrintMoney()' routine that I use all the time, in the spirit of TMTOWTDI.
    my $amount = sprintf ('%.2f',shift()); my $s = length($amount) - 6; while ($s > 0) { substr($amount,$s,0) = ","; $s -=3; } return $currency_unit.$amount;
    Cheers,
    Ben
Re: Making money look nice
by Anonymous Monk on Apr 12, 2003 at 04:20 UTC
    If I remember correctly its actually in the Number:: family.. I think.. Anywho whatever module I saw it in, it went something to the effect of..
    push(@data, '189803943', '39203', '12938032'); for ( @data ) { print "commified: " . add_c($_) . "\n"; } sub add_c { my $n = shift; $n = '0' x ( length($n) % 3 == 2 ? 1 : 2 ) . $n if (length($n) % 3) +; $n =~ s/(...)/\1,/g; $n =~ s/(?:^0+,?|,$)//g; return($n); }
    Produces:
    commified: 189,803,943
    commified: 39,203
    commified: 12,938,032
Re: Making money look nice
by reclaw (Curate) on Apr 12, 2003 at 22:04 UTC
    use Number::Format; my $nf = new Number::Format(); my $money = $nf->format_number(195000);
    also handy when dealing with user input
    $money = $nf->unformat_number($money);
    Your milage my very.
Re: Making money look nice
by Anonymous Monk on Apr 11, 2003 at 23:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://249997]
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found