On Thu, May 02, 2002 at 04:16:01PM -0700, Paul Makepeace wrote: > perl -lpe '$_ = sprintf "%.2f",$_; 1 while s/(?<=[\d])((?:\d{3})+)(?=[.,])/,$1/' > > 123.1 > 123.10 > > 5679.123 > 5,679.12 > > 1289053234.20852 > 1,289,053,234.21 > > Is there an easier way to do this? London.pm meeting taken its toll?
I'd use locale, but it's still ugly. From perllocale: # Get some of locale's numeric formatting parameters my ($thousands_sep, $grouping) = @{localeconv()}{'thousands_sep', 'grouping'}; # Apply defaults if values are missing $thousands_sep = ',' unless $thousands_sep; if ($grouping) { @grouping = unpack("C*", $grouping); } else { @grouping = (3); } # Format command line params for current locale for (@ARGV) { $_ = int; # Chop non-integer part 1 while s/(\d)(\d{$grouping[0]}($|$thousands_sep))/$1$thousands_sep$2/; print "$_"; } print "\n"; -- mike I got Nirvana in my head, I'm so glad I'm not dead