Login   /   Register

Pad Number String with Zeroes

Rate this article
     1 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 51 votes, average: 4 out of 5
Loading ... Loading ...
May 3rd, 2008 by Meir Bar-Tal

Google Search Results

You arrived here after searching for the following phrases:

Click a phrase to jump to the first occurrence, or return to the search results.

Sometimes a numbered list is required, such as a list to populate a ListView, TreeView or one to create sequential files in a folder. One of the main problems is that such lists typically sort the items lexicographically, so that numbered items end-up in the wrong order, such as File_1, File_10, FIle_11, …, File_2, FIle_20, etc.
 
The following function transforms the required number to a string with padding zeroes on the left. This is accomplished by taking the difference between the string lengths of the maximum value in the list and the given number.
 
'*******************************************************************************
Public Function PadNumber(ByVal intCurrentNum, ByVal intMaxNumInList)
'*******************************************************************************
'Description    :   Pads a number with zeroes on the left, according to the expected
'                   maximum length of the numbers in the list.
'
'Purpose        :   To keep a number list sorted properly, as with a file list (001,
'                   002,…, 010, and not 1, 10, 11,…, 2, 20).
'
'Arguments      :   intCurrentNum (the current number to be padded)
'                   intMaxNumInList (the top number in the list)
'                   Note: The arguments are always taken in absolute values
'Returns        :   The padded intCurrentNum (for example, If 1 and 9999 are sent to
'                   the function, the result will be 0001)
'
'Developed By   :   Meir Bar-Tal
'
'Date           :   11-Jan-2007
'
'*******************************************************************************
    'Validates the arguments - if invalid then it returns the value as is
    If (Not IsNumeric(intCurrentNum) Or Not IsNumeric(intMaxNumInList)) Then
        PadNumber = intCurrentNum
        Exit Function
    End If
    
    If (Abs(intCurrentNum) >= Abs(intMaxNumInList)) Then
        PadNumber = intCurrentNum
        Exit Function
    End If
 
    PadNumber = String(len(CStr(Abs(intMaxNumInList)))-len(CStr(Abs(intCurrentNum))), "0″) _
                    & CStr(Abs(intCurrentNum))
 
'*******************************************************************************
End Function
'*******************************************************************************

 

Usage examples:

Msgbox PadNumber(4, 34567)    'Returns 00004
Msgbox PadNumber(-4, 34567)   'Returns 00004 
Msgbox PadNumber(4, -34567)   'Returns 00004
Msgbox PadNumber(34567, 4)    'Returns 34567
Msgbox PadNumber(4, 9)        'Returns 4
Msgbox PadNumber("Hello", 9999) 'Returns Hello

 

You’re invited to customize this function according to your needs.

Posted in Meir Bar-Tal's Blog, VBScript

4 Responses to “Pad Number String with Zeroes”

  1. Mohamed Ali Says:

    [+]

    Thanks for the function Meir. Small correction in the examples, I think you're missing a zero, eg. Msgbox PadNumber(4, 34567) ... ...

  2. Meir Bar-Tal Says:

    [-]

    You’re right.

    Thanks for the correction, I’ve amended it.

  3. msharp Says:

    [+]

    Padding a number using the Right function: PadNumber = Right('0000' & Number, 5) If the Number is 123, PadNumber will be... ...

  4. Meir Bar-Tal Says:

    [+]

    That is true, but what's your point? The point of my function is that you just need to give the maximum number in the list and it ... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 1382 times