On Wed, 29 Aug 2001 [EMAIL PROTECTED] wrote:

> Is there an easy way or some format option that allows one to display an
> arbitrary number with commas:
>
> 12345678910 = 1,234,567,890
>
> Or do I need to set up a looping structure to split the number and insert
> commas???

Here's the solution given in the Perl Cookbook:

sub commify {

  my $text = reverse $_[0];
  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  return scalar reverse $text;
}

This reverses the string (to aoivd put commas to the right of the
decimal), works backwards in the number, then reverses it back the correct
way.  Try it!

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
Passwords are implemented as a result of insecurity.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to