cprintf "There are at least %'d ways to do it!\n", 42e6;
####
printf "There are at least @{[commify('%d')]} ways to do it!\n", 42e6;
##
##
sub commify {
local $_ = shift;
my $spc = ''; s/^(\s+)// and $spc = $1; # trim and save leading space
my $adj = 0;
$adj++ while s/^([-+]?\d+)(\d{3})/$1,$2/;
$spc =~ s/.{$adj}//; # adjust space for commas added
s/\s{0,$adj}$// if /\s$/; # adjust right padding
return $spc . $_;
}
sub cprintf {
(my $format = shift) =~
s{
\%(['+0-9.-]+)?([df]) # capture all valid %d and %f flags and modifiers
}{
my $p = $1;
my $c = $2;
$p =~ s/'// ? "\@{[commify('%$p$c')]}" : "%$p$c"
}gex;
my $str = sprintf $format, @_;
print eval "\"$str\"";
}
cprintf "%+'012d\n", 1e6;
cprintf "<%-'12.6d>\n", 1e6;
cprintf "<%-+12.6'd>\n", 1e6;
cprintf "<%+12.6'd>\n", -1e6;
cprintf "<%+12.6d>\n", -1e6;
cprintf "<%+12.2'f>\n", 1234.5;
cprintf "There are at least %'d ways to do it!\n", 42e6;
__END__
+00,001,000,000
<1,000,000 >
<+1,000,000 >
< +1,000,000>
< -1,000,000>
< -1000000>
< +1,234.50>
There are at least 42,000,000 ways to do it!