Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: front-pad a number with zero's

by japhy (Canon)
on Oct 30, 2001 at 23:27 UTC ( [id://122168]=note: print w/replies, xml ) Need Help??


in reply to front-pad a number with zero's

That's broken, due to the use of the string comparison operators (like lt) in the place of numerical comparison operators (like <). Also, a while loop can be replaced with math:
$string = ('0' x ($wanted - $current)) . $string;
But honestly, this is a job for sprintf():
sprintf "%0${len}d", $number;
That is, sprintf("%05d", 123) returns 00123.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re^2: front-pad a number with zero's
by Anonymous Monk on Jun 13, 2008 at 08:40 UTC
    You are a genius! Where did you get this sprintf "%0${len}d", $number; It's saved me a lot of troubles. 06-13-2008 Thank you.
      Where did you get this sprintf "%0${len}d", $number;
      The sprintf documentation, perhaps?
      printf '<%06s>', 12; # prints "<000012>"
      BTW: The thread you responded to is 7 years old.
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
        I'm replying to a ten year old thread!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://122168]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-25 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found