It's just not worth the hassle to patch these bugs when sprintf is already sea-worthy.sub one { my ($number) = @_; my $padded = sprintf("%09d",$number); $padded; } sub two { my ($number) = @_; my $padded = "000000000"; my $length = length($number); substr($padded,(-$length),$length,$number); $padded; } print one(-6),"\n"; # -00000006 print two(-6),"\n"; # 0000000-6 print one(43/6),"\n"; # 000000007 print two(43/6),"\n"; # 7.16666666666667
In reply to Re: sprintf vs. substr (for zero-padded values)
by tadman
in thread sprintf vs. substr (for zero-padded values)
by Theseus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |