cide
asked on
Adding zeros before numbers.
Hi,
I want a quick way to add zeros before a number to make it exactly three chars long. i.e. 7 becomes 007 and 77 becomes 077. how would I do this quickly. I know how to do it already but I have a felling you can do it in one simple line with PERL.
I want a quick way to add zeros before a number to make it exactly three chars long. i.e. 7 becomes 007 and 77 becomes 077. how would I do this quickly. I know how to do it already but I have a felling you can do it in one simple line with PERL.
best:
print sprintf("%03d", $num),"\n";
because it handles negative numbers, numbers > 1000, and fractions as you'd expect.
you could also try
print substr('00'.$num,-3),"\n";
print substr(1000+$num,-3),"\n";
print sprintf("%03d", $num),"\n";
because it handles negative numbers, numbers > 1000, and fractions as you'd expect.
you could also try
print substr('00'.$num,-3),"\n";
print substr(1000+$num,-3),"\n";
ASKER
Thanks!
ASKER
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cide, did you really prefer patowik's answer to alamo's?
ASKER
Not really, but alamo's last post was three days ago and he commented not answered. I'd rather give alamo the points though.
ASKER