checking that numbers are in required format before using them

Posted on 2009-04-27
Last Modified: 2012-05-06
I have a script that does some string concatenation that includes numbers that get pulled from various places. Before I can use them, however, I need all the numbers to be in a consistent  format (usually, ###) so that they sort properly.

How do you write an IF statement that checks adds leading zeros as needed so that 1,10,100 become 001,010,100?

Thanks in advance.

Question by:Leprechaun
    LVL 25

    Accepted Solution

    Hi Leprechaun,

    I would use a sprintf ... e.g.

    $x = sprintf "%03d", $y;

    In fact, you probably could use the sprintf to do all of your concatentation.


    Author Closing Comment

    Great, thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now