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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |