Solved

Adding zeros before numbers.

Posted on 1998-07-19
7
178 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

839 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