From number to clearer string

Discussion in 'Perl Misc' started by * Tong *, Jul 25, 2005.

  1. * Tong *

    * Tong * Guest

    Hi,

    Is any Perl modules that can translate number like 10987654321 into
    clearer to read string 10,987,654,321? and back?

    I don't know what this kind of transformation called, so I don't know how
    to search. thanks.

    --
    Tong (remove underscore(s) to reply)
    *niX Power Tools Project: http://xpt.sourceforge.net/
    - All free contribution & collection
     
    * Tong *, Jul 25, 2005
    #1
    1. Advertising

  2. * Tong *

    Todd Guest

    * Tong * wrote:
    > Hi,
    >
    > Is any Perl modules that can translate number like 10987654321 into
    > clearer to read string 10,987,654,321? and back?
    >
    > I don't know what this kind of transformation called, so I don't know how
    > to search. thanks.
    >


    Hmmm, number and commas

    what if I try

    perldoc -q commas

    Found in /apps/perl/5.9.1/lib/5.9.1/pod/perlfaq5.pod
    How can I output my numbers with commas added?

    This subroutine will add commas to your number:

    sub commify {
    local $_ = shift;
    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
    return $_;
    }

    This regex from Benjamin Goldberg will add commas to
    numbers:

    s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;

    It is easier to see with comments:

    s/(
    ^[-+]? # beginning of number.
    \d{1,3}? # first digits before first comma
    (?= # followed by, (but not included
    in the match) :
    (?>(?:\d{3})+) # some positive multiple of three
    digits.
    (?!\d) # an *exact* multiple, not x * 3
    + 1 or whatever.
    )
    | # or:
    \G\d{3} # after the last group, get three
    digits
    (?=\d) # but they have to have more
    digits after them.
    )/$1,/xg;
     
    Todd, Jul 25, 2005
    #2
    1. Advertising

  3. * Tong *

    Paul Lalli Guest

    * Tong * wrote:
    > Is any Perl modules that can translate number like 10987654321 into
    > clearer to read string 10,987,654,321? and back?
    >
    > I don't know what this kind of transformation called, so I don't know how
    > to search. thanks.


    How about the word "commas"?

    perldoc -q commas
    "How can I output my numbers with commas added?"

    (Removing the commas again is a pretty trivial use of the tr///
    operator)

    Paul Lalli
     
    Paul Lalli, Jul 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. exquisitus
    Replies:
    4
    Views:
    8,393
    Patricia Shanahan
    May 6, 2005
  2. Replies:
    5
    Views:
    556
    Pete Becker
    May 21, 2005
  3. Ed Davis
    Replies:
    4
    Views:
    466
    James Hu
    Oct 3, 2003
  4. davidj411
    Replies:
    11
    Views:
    1,339
    Kam-Hung Soh
    May 29, 2008
  5. spiralofhope

    Clearer errors in 1.9's minitest

    spiralofhope, Mar 26, 2011, in forum: Ruby
    Replies:
    6
    Views:
    189
    spiralofhope
    Mar 28, 2011
Loading...

Share This Page