print 001 instead of 1

Hello all,
I have a variable named n. n is a number from 0 to 999. I want to print
it with always 3 digits (like 002 instead 2). How do I do it? If I use
printf ("%3d",$n), it uses spaces...

Best regards,
-vcf
Vitor Flausino [ Mi, 30 März 2005 15:37 ] [ ID #721769 ]

Re: print 001 instead of 1

In article <424aab9f$0$6570$4d4efb8e [at] read.news.pt.uu.net>, Vitor
Flausino <vflausino [at] dti.pga.aero> wrote:

> Hello all,
> I have a variable named n. n is a number from 0 to 999. I want to print
> it with always 3 digits (like 002 instead 2). How do I do it? If I use
> printf ("%3d",$n), it uses spaces...

Try printf("%3.3d,$n);


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jim Gibson [ Mi, 30 März 2005 21:07 ] [ ID #721770 ]

Re: print 001 instead of 1

Vitor Flausino wrote:

> printf ("%3d",$n), it uses spaces...

Sounds like you haven't looked at the first example shown by
perldoc -f sprintf

-Joe
Joe Smith [ Mi, 30 März 2005 21:34 ] [ ID #721771 ]

Re: print 001 instead of 1

On Wed, 30 Mar 2005 11:07:27 -0800, Jim Gibson
<jgibson [at] mail.arc.nasa.gov> wrote:

>In article <424aab9f$0$6570$4d4efb8e [at] read.news.pt.uu.net>, Vitor
>Flausino <vflausino [at] dti.pga.aero> wrote:
>
>> Hello all,
>> I have a variable named n. n is a number from 0 to 999. I want to print
>> it with always 3 digits (like 002 instead 2). How do I do it? If I use
>> printf ("%3d",$n), it uses spaces...
>
>Try printf("%3.3d,$n);

or rather
printf("%03d",$n);

--
mvh/Regards Kåre Olai Lindbach
(News: Remove '_delete_' and '.invalid')
(HTML-written email from unknown will be discarded)
barbr-en_delete_ [ Mi, 30 März 2005 21:34 ] [ ID #721772 ]

Re: print 001 instead of 1

"Vitor Flausino" <vflausino [at] dti.pga.aero> wrote in message
news:424aab9f$0$6570$4d4efb8e [at] read.news.pt.uu.net...
> Hello all,
> I have a variable named n. n is a number from 0 to 999. I want to print it
> with always 3 digits (like 002 instead 2). How do I do it? If I use printf
> ("%3d",$n), it uses spaces...


printf "%03d",$n;
Tintin [ Do, 31 März 2005 09:21 ] [ ID #723715 ]
Perl » alt.perl » print 001 instead of 1

Vorheriges Thema: Yet Another Perl Conference, North America, 2005
Nächstes Thema: how to handle global variable?