Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Number format
by FunkyMonk (Bishop) on Nov 18, 2007 at 22:34 UTC
    There's nothing built-in, but it's fairly easy to achieve the effect with a regexp.

    A search for "commify" yields plenty of reading material.

    update

    Here's the code I use which originated (IIRC) from comp.lang.perl.misc many years ago:

    sub commify { return $_[0] if $_[0] !~ m/^\d*?[.]?\d*$/; ( my $input = reverse 0+$_[0] ) =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,> +g; return scalar reverse $input; }

Re: Number format
by ww (Archbishop) on Nov 18, 2007 at 22:47 UTC

    Not built-in....

    my $text = 12345678.9; commify($text); print "\n |$text|\n"; # after Perl Cookbook recipe 2.17 sub commify { my $text = reverse $text; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; print "In the sub: " . reverse $text . "\n"; }
    Update: As FunkyMonk noted (types faster than I): </update>
    Commify numbers, the boring and straightforward way
    and
    "Commifying" a number
    may shed more light on this matter... as will his suggestion to use "Search" as a first step to obtaining the wisdom here in the Monastery.

    FWIW, searching for "commafy" will also lead you to a thread with a useful node ...and, there's help at the ends of your fingers as well:
    see perldoc -q comma.

Re: Number format
by quester (Vicar) on Nov 19, 2007 at 06:38 UTC
    Please don't remove your original question when you update your posts. The questions and answers are a permanent part of PerlMonks, so that in the future someone who has a question like yours can find the discussion with a Super Search. This only really works if the questions can still be seen.