Solved

Adding zeros before numbers.

Posted on 1998-07-19
7
173 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
Comment Utility
Edited text of question
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Comment Utility
Thanks!
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:cide
Comment Utility
Thanks!
0
 
LVL 1

Accepted Solution

by:
patowic earned 10 total points
Comment Utility
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
Comment Utility
cide, did you really prefer patowik's answer to alamo's?
0
 

Author Comment

by:cide
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl rename 2 137
Filtering a file to table 9 98
Formatting stings with pack and printf in perl 5 48
Perl Frameworks 1 51
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

12 Experts available now in Live!

Get 1:1 Help Now