Solved

Adding zeros before numbers.

Posted on 1998-07-19
7
182 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:cide
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:cide
ID: 1208508
Edited text of question
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208509
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";
0
 

Author Comment

by:cide
ID: 1208510
Thanks!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cide
ID: 1208511
Thanks!
0
 
LVL 1

Accepted Solution

by:
patowic earned 10 total points
ID: 1208512
If you don't want to sprintf, there's this little kludge (assuming $mynum is the number you want to prepend with zeros)

if ($mynum < 10) { $mynum = "000$mynum"}
elsif ($mynum < 100) { $mynum = "00$mynum"}
elsif ($mynum < 1000) { $mynum = "0$mynum"}

bear in mind, with a negative this will result in something like -7 ===> 000-7


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208513
cide, did you really prefer patowik's answer to alamo's?
0
 

Author Comment

by:cide
ID: 1208514
Not really, but alamo's last post was three days ago and he commented not answered.  I'd rather give alamo the points though.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question