Solved

Adding zeros before numbers.

Posted on 1998-07-19
7
176 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now