This is what we use. It is based on an RE devised by merlyn with a patch to fix issues with arbitrarily long decimal components ie stop it commifying after the decimal point. It expects to be fed numbers rather than arbitrary strings containing numbers. To use the basic function on arbirary strings you first need to extract the numbers and process them sequentially which you can do with a /ge RE.

print add_commas( '+1234567.123456789010'), $/; print add_commas_arb_string( 'Hello +1234567.123456 Hello -1234567.123 +456' ); sub add_commas { my ( $number ) = @_; return undef unless $number; ( $number, my $dec ) = $number =~ m!([+-]?\d+)\.?(\d*)!; return undef unless $number; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1,/g if length($number) > 3; return $dec ? "$number.$dec" : $number; } sub add_commas_arb_string { my ( $string ) = @_; $string =~ s/([+-]?\d(?:\d*\.\d+|\d*))/add_commas($1)/ge; return $string; } __DATA__ +1,234,567.123456789010 Hello +1,234,567.123456 Hello -1,234,567.123456

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Comma-fy floats with (+/-)look(aheads/behinds) by tachyon
in thread Comma-fy floats with (+/-)look(aheads/behinds) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.