Link to home
Start Free TrialLog in
Avatar of cide
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.
Avatar of cide
cide

ASKER

Edited text of question
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";
Avatar of cide

ASKER

Thanks!
Avatar of cide

ASKER

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of patowic
patowic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
cide, did you really prefer patowik's answer to alamo's?
Avatar of cide

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.