The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
How to format a string or number with leading zeros?
Discuss How to format a string or number with leading zeros? in the Perl Programming forum on Dev Shed. How to format a string or number with leading zeros? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 13th, 2001, 03:47 PM
|
|
Registered User
|
|
Join Date: Jun 1999
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I am new to Perl. Trying to print out a number padded with leading zeros.
example:
$num = 123;
the output/print should have 00123.Total width of 5 padded with leading zeros.
Appreciate any help in this direction.
Thank you
|

January 13th, 2001, 04:09 PM
|
 |
Ole` Timer
|
|
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 472

Time spent in forums: 5 h 29 m 6 sec
Reputation Power: 13
|
|
Quote: Originally posted by kumars
I am new to Perl. Trying to print out a number padded with leading zeros.
example:
$num = 123;
the output/print should have 00123.Total width of 5 padded with leading zeros.
Appreciate any help in this direction.
Thank you
|
Try this:
Code:
my $num = "123";
$num = sprintf("%5d", $num);
$num=~ tr/ /0/;
this will "pad" the variable $num with leading "0"'s up to 5 characters: IE 00123 or 01234. If for some reason the $num contains a total of 5 characters, then no "0"'s would be added.
Hope this helps
Mickalo
|

January 13th, 2001, 05:07 PM
|
|
Registered User
|
|
Join Date: Jun 1999
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Great, it worked.
Thank you Mickalo,
It worked.Thanks again for the fast response.
|

January 13th, 2001, 05:49 PM
|
 |
Ole` Timer
|
|
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 472

Time spent in forums: 5 h 29 m 6 sec
Reputation Power: 13
|
|
Hey,.. No problems
Glad to help when I can.
Mickalo
|

January 18th, 2007, 12:54 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 1
Time spent in forums: 4 m 28 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by mickalo Try this:
Code:
my $num = "123";
$num = sprintf("%5d", $num);
$num=~ tr/ /0/;
|
Code:
my $num = "123";
$num = sprintf("%05d", $num);
|

January 19th, 2007, 01:57 AM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 3
Time spent in forums: 3 h 54 m 11 sec
Reputation Power: 0
|
|
|
You can do "substr("00000$num",-5)" as well.
It might be a bit faster though it won't be attractive.
|

January 19th, 2007, 02:39 AM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
Quote: | Originally Posted by Correction
Code:
my $num = "123";
$num = sprintf("%05d", $num);
|
hehehe... you corrected a 6 year old post! That must be a record. 
|

May 25th, 2010, 09:17 AM
|
|
Registered User
|
|
Join Date: May 2010
Posts: 1
Time spent in forums: 4 m 31 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by KevinADC hehehe... you corrected a 6 year old post! That must be a record.  |
It helped me. 
|

January 29th, 2013, 11:40 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Time spent in forums: 3 m 5 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by wdrev It helped me.  |
3 Years later, It helped me too 
|

February 2nd, 2013, 06:50 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 1 m 32 sec
Reputation Power: 0
|
|
|
It helped me too
|

February 4th, 2013, 05:20 PM
|
|
|
Hi,
well I had a very similar problem today at work and I originally used a solution similar to what I thought was the best solution shown so far:
Perl Code:
Original
- Perl Code |
|
|
|
And it does work as long as you are guaranteed to have digits in your input. Except that when I ran the program, I got an error message because there were a couple of cases where I have letters rather than digits in my input file. And the sprinft formatting string fails in this case. These letters are not really an error, but the indication of a special exception condition that still needs to be handled.
So I think I'll change it to:
Perl Code:
Original
- Perl Code |
|
|
|
so that if my input is "foo", I'll get "00foo" which, even if it does not seem to make too much sense, is what I need in this case to have the logic of my program working in this case (I need to pad my data with 0's at the beginning of my value, in order to be able to compare data from 2 different files with similar values but different formatting conventions.
This was just to say that the "%05s" format string might just be more general than "%05d" in some real life situations. I haven't tested it against my data yet, I'll do that tomorrow morning.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|