in reply to Can I pad a number with leading spaces rather than leading 0's

I've used something like this before, in little non-production scripts:
for my $amount ('60.00','160.00','5') { print "\$", pad(dollarize($amount),6), "\n"; } sub pad { my $txt = shift; my $num = shift || 7; return " " x ($num - length($txt)) . $txt; } sub dollarize { my $txt = shift; $txt .= '.' unless $txt =~ /\./; $txt .= '0' unless $txt =~ /\.\d\d/; $txt .= '0' unless $txt =~ /\.\d\d/; return $txt; }

-Blake

  • Comment on Re: Can I pad a number with leading spaces rather than leading 0's
  • Download Code