Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

front-pad a number with zero's

by deprecated (Priest)
on Oct 30, 2001 at 23:22 UTC ( #122165=snippet: print w/ replies, xml ) Need Help??

Description: Gee, where'd my post go? <!- for all you people who downvoted working code that was clear and easy to understand, have your fucking monastery. you dont have to downvote because it was done fucking differently. blow me. -!>
 
Comment on front-pad a number with zero's
Re: front-pad a number with zero's
by japhy (Canon) on Oct 30, 2001 at 23:27 UTC
    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:??;

      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]
Re: front-pad a number with zero's
by Masem (Monsignor) on Oct 30, 2001 at 23:27 UTC
    Why not (simply)?:
    $string = sprintf( "%05d", $number );

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: front-pad a number with zero's
by lhoward (Vicar) on Oct 30, 2001 at 23:27 UTC
    You can do the same thing with sprintf.
    my $f=sprintf("%05d",123);
    will put "00123" into $f.
(Ovid) Re: front-pad a number with zero's
by Ovid (Cardinal) on Oct 30, 2001 at 23:29 UTC

    TIMTOWTDI (though sprintf is faster):

    sub pad_zeros { my $optimal_length = 5; my $num = shift; $num =~s/^(\d+)$/("0"x($optimal_length-length$1)).$1/e; return $num; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      A regex is overkill here. (If I'm saying it, trust me...) If you're against using sprintf(), just do the math:
      sub pad { my ($num, $len) = @_; return '0' x ($len - length $num) . $num; }

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

(tye)Re: front-pad a number with zero's
by tye (Cardinal) on Oct 31, 2001 at 00:18 UTC

    To both pad short values and (left)truncate long ones: $str= substr("0"x$len.$str,-$len);
    is another WTDI.

            - tye (but my friends call me "Tye")

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-04-19 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls