Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
We don't bite newbies here... much
 
PerlMonks  

Integer to Money format

by vinoth.ree (Vicar)
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

Example:

INPUT:

4123456789

OUTPUT:

4,12,34,56,789

How to do it in regular expression?

The input length may increase!

Comment on Integer to Money format
Re: Integer to Money format
by happy.barney (Pilgrim) 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 lyklev (Pilgrim) on Jan 20, 2011 at 08:06 UTC

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

    4,123,456,789

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

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

      CountZero

      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 toolic (Chancellor) 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 sundialsvc4 (Monsignor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-04-21 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls