Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Problems with zero filling a number.

by dwatson06 (Friar)
on Jul 15, 2002 at 18:42 UTC ( #181873=perlquestion: print w/ replies, xml ) Need Help??
dwatson06 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a set of numbers to a file so that another programmer may reference them. This number needs to be zero filled with 9 digits. I can use the following to print out on the command line...
my $getNumber = 213;
printf "%09d$getNumber";
And it prints fine but when I write to file it writes%09d213.
Is there a simple sub included to pad numbers (or zero fill) to a given length?
Thank you,
Daniel

Comment on Problems with zero filling a number.
Re: Problems with zero filling a number.
by broquaint (Abbot) on Jul 15, 2002 at 18:52 UTC
    Try re-reading the docs for printf() (which may require reading the docs for sprintf())
    printf "%09d", 213; __output__ 000000213

    HTH

    _________
    broquaint

Re: Problems with zero filling a number.
by thelenm (Vicar) on Jul 15, 2002 at 18:54 UTC
    You'll want to pass $getNumber as a separate argument to printf (or sprintf), like this:
    my $getNumber = 213; my $padded = sprintf("%09d", $getNumber);

    -- Mike

    --
    just,my${.02}

Re: Problems with zero filling a number.
by Abigail-II (Bishop) on Jul 16, 2002 at 12:08 UTC
    Very, very strange. Are you sure that you did a printf? Not that your code is correct as others have pointed out, but it shouldn't write %09d213 either. I've tried it on all versions of Perl released since 5.000, and they all print 000000000213. From 5.7.1 onwards, it will print a warning as well.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181873]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2015-07-05 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls