Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Integer to Money format

by vinoth.ree (Monsignor)
on Jan 20, 2011 at 06:07 UTC ( #883255=perlquestion: print w/replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I have a integer number that should be represented as money format






How to do it in regular expression?

The input length may increase!

Replies are listed 'Best First'.
Re: Integer to Money format
by happy.barney (Friar) on Jan 20, 2011 at 07:00 UTC
    my $n = '4123456789'; $n =~ s/(?<= \d )(?= (?:\d{2})* \d{3} $ )/,/gx;

      Thank you very much!

Re: Integer to Money format
by toolic (Bishop) on Jan 20, 2011 at 13:45 UTC
    Whenever I see "format" and "number", I think of... Number::Format :)
    use warnings; use strict; use Number::Format qw(format_picture); print format_picture(4123456789, '##,##,##,##,##,##,###'), "\n"; __END__ 4,12,34,56,789
    You could consider this as an alternative to regular expressions. One disadvantage is that format_picture forces you to know the upper bound on your number's length.
Re: Integer to Money format
by lyklev (Pilgrim) on Jan 20, 2011 at 08:06 UTC

    That does not look like money format. The output would be


    and possibly followed by a period and two digits for cents.

      It is the Indian way of formatting money amounts. See: Indian Numbering System


      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Learned something today, thanks!
Re: Integer to Money format
by chrestomanci (Priest) on Jan 20, 2011 at 09:04 UTC

    Was that a homework question? It looks a bit contrived to do it as a regular expression, when there alternative ways of inserting commas every three digits. For example:

    join(',', '4123456789' =~ m/.{3}/g )
      did you mean something like this ? :-)
      scalar reverse join(',', (reverse '4123456789') =~ m/.{1,3}/g);
Re: Integer to Money format
by sundialsvc4 (Abbot) on Jan 20, 2011 at 15:05 UTC

    When faced with a problem like that, I write a sub to do it. I fill that subroutine with comments, and I write the routine to be Perfectly Clear. Not Fast, not Short, and especially not Clever.

      Oh really? Is that what You Do?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://883255]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-23 21:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.