Solved

Adding zeros before numbers.

Posted on 1998-07-19
7
177 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extract multiple value with delimiters from a string 4 170
Excel to CSV conversion with specific columns 5 82
Using Perl DBI to query oracle 3 43
PERL - Find newest folder 12 138
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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