in reply to adding comma and two decimals
..prints..sub numfix { my $num = shift; $num = sprintf("%.2f", $num); 1 while $num =~ s/^([-+]?\d+)(\d{3})/$1,$2/; return $num; } foreach ( qw( 1000 1024 4.3 2.44 1024.44 ) ) { print( $_ ); print( " => " ); print( numfix( $_ ) ); print( "\n" ); }
1000 => 1,000.00 1024 => 1,024.00 4.3 => 4.30 2.44 => 2.44 1024.44 => 1,024.44
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: adding comma and two decimals
by kulls (Hermit) on Nov 25, 2005 at 04:17 UTC | |
|
Re^2: adding comma and two decimals
by monarch (Priest) on Nov 25, 2005 at 02:18 UTC | |
by Anonymous Monk on Nov 25, 2005 at 02:20 UTC |