Windows Vista Forums

is there an equivalent Function to FORMAT(56,"0000")
  1. #1


    magellan Guest

    is there an equivalent Function to FORMAT(56,"0000")

    Hi All,


    I have a string variable that I increment every time thru the loop by +1.

    I want to format the string as "0056", with leading zeros and always a fixed
    four(4) char field.

    In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    anything equivalent to this is vbs...? or some sample code to create my own
    Function...?

    thanks in advance


    Magellan






      My System SpecsSystem Spec

  2. #2


    James Whitlow Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    "magellan" <aljee@xxxxxx> wrote in message
    news:%23EfjIRuYJHA.552@xxxxxx

    > Hi All,
    >
    > I have a string variable that I increment every time thru the loop by +1.
    >
    > I want to format the string as "0056", with leading zeros and always a
    > fixed four(4) char field.
    >
    > In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    > anything equivalent to this is vbs...? or some sample code to create my
    > own Function...?
    I am not aware of a built-in function in VBScript to do this. Others in
    the group might, though.

    See if the below function does what you want:

    Function Format(ByVal iNum, ByVal sFormat)
    Format = Right(sFormat & iNum, Len(sFormat))
    End Function



      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")


    "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
    news:%23jnQAeuYJHA.4456@xxxxxx

    > "magellan" <aljee@xxxxxx> wrote in message
    > news:%23EfjIRuYJHA.552@xxxxxx

    >> Hi All,
    >>
    >> I have a string variable that I increment every time thru the loop by +1.
    >>
    >> I want to format the string as "0056", with leading zeros and always a
    >> fixed four(4) char field.
    >>
    >> In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    >> anything equivalent to this is vbs...? or some sample code to create my
    >> own Function...?
    >
    > I am not aware of a built-in function in VBScript to do this. Others in
    > the group might, though.
    >
    > See if the below function does what you want:
    >
    > Function Format(ByVal iNum, ByVal sFormat)
    > Format = Right(sFormat & iNum, Len(sFormat))
    > End Function
    >
    VBScript does not support the VB Format function. You need to code your own,
    as James has done.

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

  4. #4


    Jim de Graff Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    The following function formats a number in a field of the given width and
    pads with the given character. In this case, L, R, or C determines whether
    the number will be positioned at the left, right or ventre of the field.

    wscript.echo Pad("L","0",53,9)
    wscript.echo Pad("R","-",53,9)
    wscript.echo Pad("C","-",53,9)

    Function Pad ( LRC , PadChar , Num , Width )

    dim padstr

    'if number equals or exceeds width then return number as string

    if Len(Num) >= Width then
    Pad = cstr(Num)
    Exit Function
    End If

    'calculate pad string for desired width

    padstr = Replace(Space(Width-Len(Num))," ",PadChar)

    'format number on left, right or centre and pad accordingly

    select case Ucase(LRC)
    case "L" : Pad = num & padstr
    case "R" : Pad = padstr & num
    case "C" : Pad = Left(Left(padstr,Len(padstr)\2) & Num & padstr,Width)
    case else: : Pad = num
    end select

    End Function



    "magellan" <aljee@xxxxxx> wrote in message
    news:%23EfjIRuYJHA.552@xxxxxx

    > Hi All,
    >
    >
    > I have a string variable that I increment every time thru the loop by +1.
    >
    > I want to format the string as "0056", with leading zeros and always a
    > fixed four(4) char field.
    >
    > In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    > anything equivalent to this is vbs...? or some sample code to create my
    > own Function...?
    >
    > thanks in advance
    >
    >
    > Magellan
    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    Todd Vargo Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    magellan wrote:

    > Hi All,
    >
    >
    > I have a string variable that I increment every time thru the loop by +1.
    >
    > I want to format the string as "0056", with leading zeros and always a
    fixed

    > four(4) char field.
    >
    > In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    > anything equivalent to this is vbs...? or some sample code to create my
    own

    > Function...?
    For a fixed string of 4 places as stated.

    MsgBox Pad(56)

    Function Pad(number)
    Pad = Right("0000" & number, 4)
    End Function



    Adding some versatility you might prefer this.

    MsgBox ZeroPad(56, 4)

    Function ZeroPad(number, digits)
    If Len(number) > digits Then
    ZeroPad = number
    Else
    ZeroPad = Right(String(digits, "0") & number, digits)
    End If
    End Function

    --
    Todd Vargo
    (Post questions to group only. Remove "z" to email personal messages)


      My System SpecsSystem Spec

  6. #6


    Jim de Graff Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    Ah yes. String. that simplifies the previous code to

    wscript.echo Pad("L","0",53,9)
    wscript.echo Pad("R","-",53,9)
    wscript.echo Pad("C","-",53,9)

    Function Pad ( LRC , PadChar , Num , Width )

    dim padstr

    'if number equals or exceeds width then return number as string

    if Len(Num) >= Width then
    Pad = cstr(Num)
    Exit Function
    End If

    'calculate pad string for desired width

    padstr = String(Width-Len(Num),PadChar)

    'format number on left, right or centre and pad accordingly

    select case Ucase(LRC)
    case "L" : Pad = num & padstr
    case "R" : Pad = padstr & num
    case "C" : Pad = Left(Left(padstr,Len(padstr)\2) & Num & padstr,Width)
    case else: : Pad = num
    end select

    End Function


    "Todd Vargo" <tlvargo@xxxxxx> wrote in message
    news:O58jYUyYJHA.1336@xxxxxx

    > magellan wrote:

    >> Hi All,
    >>
    >>
    >> I have a string variable that I increment every time thru the loop by +1.
    >>
    >> I want to format the string as "0056", with leading zeros and always a
    > fixed

    >> four(4) char field.
    >>
    >> In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    >> anything equivalent to this is vbs...? or some sample code to create my
    > own

    >> Function...?
    >
    > For a fixed string of 4 places as stated.
    >
    > MsgBox Pad(56)
    >
    > Function Pad(number)
    > Pad = Right("0000" & number, 4)
    > End Function
    >
    >
    >
    > Adding some versatility you might prefer this.
    >
    > MsgBox ZeroPad(56, 4)
    >
    > Function ZeroPad(number, digits)
    > If Len(number) > digits Then
    > ZeroPad = number
    > Else
    > ZeroPad = Right(String(digits, "0") & number, digits)
    > End If
    > End Function
    >
    > --
    > Todd Vargo
    > (Post questions to group only. Remove "z" to email personal messages)
    >


      My System SpecsSystem Spec

  7. #7


    Richard Mueller [MVP] Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")


    "magellan" <aljee@xxxxxx> wrote in message
    news:%23EfjIRuYJHA.552@xxxxxx

    > Hi All,
    >
    >
    > I have a string variable that I increment every time thru the loop by +1.
    >
    > I want to format the string as "0056", with leading zeros and always a
    > fixed four(4) char field.
    >
    > In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    > anything equivalent to this is vbs...? or some sample code to create my
    > own Function...?
    >
    There must be better ways to do this, but here is a VBScript function that
    handles more numeric formats. This does not handle dates or negative numbers
    in parentheses:
    =========
    Wscript.Echo Format(12345.67, "##,#00.00")
    Wscript.Echo Format(12345.67, "$##,#00.00")
    Wscript.Echo Format(2345.67, "$##,#00.00")
    Wscript.Echo Format(345.67, "$##,#00.00")
    Wscript.Echo Format(45.67, "$##,#00.00")
    Wscript.Echo Format(5.67, "$##,#00.00")
    Wscript.Echo Format(2345.6, "$##,#00.00")
    Wscript.Echo Format(12345, "$##,#00.00")
    Wscript.Echo Format(-2345.67, "$##,#00.00")
    Wscript.Echo Format(-345.67, "$##,#00.00")

    Function Format(ByVal dblValue, ByVal strPattern)
    ' Function to format a number.
    Dim intPatternPoint, strPatternLeft, strPatternRight
    Dim strValueLeft, strValueRight
    Dim strValue, intValuePoint
    Dim j, k, blnLeading, strValueChar, strPatternChar

    strValue = CStr(CDbl(dblValue))
    intPatternPoint = InStr(strPattern, ".")
    If (intPatternPoint > 0) Then
    strPatternLeft = Left(strPattern, intPatternPoint - 1)
    strPatternRight = Mid(strPattern, intPatternPoint + 1)
    Else
    strPatternLeft = strPattern
    strPatternRight = ""
    End If
    intValuePoint = InStr(strValue, ".")
    If (intValuePoint > 0) Then
    strValueLeft = Left(strValue, intValuePoint - 1)
    strValueRight = Mid(strValue, intValuePoint + 1)
    Else
    strValueLeft = strValue
    strValueRight = ""
    End If
    strPatternLeft = StrReverse(strPatternLeft)
    strValueLeft = StrReverse(strValueLeft)
    Format = ""
    blnLeading = False
    k = 1
    For j = 1 To Len(strPatternLeft)
    If (k <= Len(strValueLeft)) Then
    strValueChar = Mid(strValueLeft, k, 1)
    Else
    blnLeading = True
    strValueChar = ""
    End If
    strPatternChar = Mid(strPatternLeft, j, 1)
    Select Case strPatternChar
    Case "0"
    If (blnLeading = True) Then
    Format = "0" & Format
    Else
    Format = strValueChar & Format
    End if
    k = k + 1
    Case "#"
    If (blnLeading = False) Then
    Format = strValueChar & Format
    End If
    k = k + 1
    Case "$"
    Format = strPatternChar & Format
    Case Else
    If (blnLeading = False) Then
    Format = strPatternChar & Format
    End If
    End Select
    Next
    If (intPatternPoint > 0) Then
    Format = Format & "."
    End If
    k = 1
    For j = 1 To Len(strPatternRight)
    If (k <= Len(strValueRight)) Then
    strValueChar = Mid(strValueRight, k, 1)
    Else
    strValueChar = ""
    End If
    strPatternChar = Mid(strPatternRight, j, 1)
    Select Case strPatternChar
    Case "0"
    If (strValueChar = "") Then
    Format = Format & "0"
    Else
    Format = Format & strValueChar
    End If
    k = k + 1
    Case "#"
    Format = Format & strValueChar
    k = k + 1
    Case Else
    Format = Format & strPatternChar
    End Select
    Next
    Format = Replace(Format, "-,", "-")
    Format = Replace(Format, "$-", "-$")

    End Function
    ======
    I appreciate the VB function more now.

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

  8. #8


    Jim de Graff Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    OK. You win.

    "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
    message news:uBvBUG9YJHA.2124@xxxxxx

    >
    > "magellan" <aljee@xxxxxx> wrote in message
    > news:%23EfjIRuYJHA.552@xxxxxx

    >> Hi All,
    >>
    >>
    >> I have a string variable that I increment every time thru the loop by +1.
    >>
    >> I want to format the string as "0056", with leading zeros and always a
    >> fixed four(4) char field.
    >>
    >> In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    >> anything equivalent to this is vbs...? or some sample code to create my
    >> own Function...?
    >>
    >
    > There must be better ways to do this, but here is a VBScript function that
    > handles more numeric formats. This does not handle dates or negative
    > numbers in parentheses:
    > =========
    > Wscript.Echo Format(12345.67, "##,#00.00")
    > Wscript.Echo Format(12345.67, "$##,#00.00")
    > Wscript.Echo Format(2345.67, "$##,#00.00")
    > Wscript.Echo Format(345.67, "$##,#00.00")
    > Wscript.Echo Format(45.67, "$##,#00.00")
    > Wscript.Echo Format(5.67, "$##,#00.00")
    > Wscript.Echo Format(2345.6, "$##,#00.00")
    > Wscript.Echo Format(12345, "$##,#00.00")
    > Wscript.Echo Format(-2345.67, "$##,#00.00")
    > Wscript.Echo Format(-345.67, "$##,#00.00")
    >
    > Function Format(ByVal dblValue, ByVal strPattern)
    > ' Function to format a number.
    > Dim intPatternPoint, strPatternLeft, strPatternRight
    > Dim strValueLeft, strValueRight
    > Dim strValue, intValuePoint
    > Dim j, k, blnLeading, strValueChar, strPatternChar
    >
    > strValue = CStr(CDbl(dblValue))
    > intPatternPoint = InStr(strPattern, ".")
    > If (intPatternPoint > 0) Then
    > strPatternLeft = Left(strPattern, intPatternPoint - 1)
    > strPatternRight = Mid(strPattern, intPatternPoint + 1)
    > Else
    > strPatternLeft = strPattern
    > strPatternRight = ""
    > End If
    > intValuePoint = InStr(strValue, ".")
    > If (intValuePoint > 0) Then
    > strValueLeft = Left(strValue, intValuePoint - 1)
    > strValueRight = Mid(strValue, intValuePoint + 1)
    > Else
    > strValueLeft = strValue
    > strValueRight = ""
    > End If
    > strPatternLeft = StrReverse(strPatternLeft)
    > strValueLeft = StrReverse(strValueLeft)
    > Format = ""
    > blnLeading = False
    > k = 1
    > For j = 1 To Len(strPatternLeft)
    > If (k <= Len(strValueLeft)) Then
    > strValueChar = Mid(strValueLeft, k, 1)
    > Else
    > blnLeading = True
    > strValueChar = ""
    > End If
    > strPatternChar = Mid(strPatternLeft, j, 1)
    > Select Case strPatternChar
    > Case "0"
    > If (blnLeading = True) Then
    > Format = "0" & Format
    > Else
    > Format = strValueChar & Format
    > End if
    > k = k + 1
    > Case "#"
    > If (blnLeading = False) Then
    > Format = strValueChar & Format
    > End If
    > k = k + 1
    > Case "$"
    > Format = strPatternChar & Format
    > Case Else
    > If (blnLeading = False) Then
    > Format = strPatternChar & Format
    > End If
    > End Select
    > Next
    > If (intPatternPoint > 0) Then
    > Format = Format & "."
    > End If
    > k = 1
    > For j = 1 To Len(strPatternRight)
    > If (k <= Len(strValueRight)) Then
    > strValueChar = Mid(strValueRight, k, 1)
    > Else
    > strValueChar = ""
    > End If
    > strPatternChar = Mid(strPatternRight, j, 1)
    > Select Case strPatternChar
    > Case "0"
    > If (strValueChar = "") Then
    > Format = Format & "0"
    > Else
    > Format = Format & strValueChar
    > End If
    > k = k + 1
    > Case "#"
    > Format = Format & strValueChar
    > k = k + 1
    > Case Else
    > Format = Format & strPatternChar
    > End Select
    > Next
    > Format = Replace(Format, "-,", "-")
    > Format = Replace(Format, "$-", "-$")
    >
    > End Function
    > ======
    > I appreciate the VB function more now.
    >
    > --
    > Richard Mueller
    > MVP Directory Services
    > Hilltop Lab - http://www.rlmueller.net
    > --
    >
    >


      My System SpecsSystem Spec

  9. #9


    magellan Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    thx for all the IDEAS...! mucho gracias...and Happy Holidays!!!

    "Jim de Graff" <rjdegraff@xxxxxx> wrote in message
    news:u6mfEyIZJHA.3496@xxxxxx

    > OK. You win.
    >
    > "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
    > message news:uBvBUG9YJHA.2124@xxxxxx

    >>
    >> "magellan" <aljee@xxxxxx> wrote in message
    >> news:%23EfjIRuYJHA.552@xxxxxx

    >>> Hi All,
    >>>
    >>>
    >>> I have a string variable that I increment every time thru the loop by
    >>> +1.
    >>>
    >>> I want to format the string as "0056", with leading zeros and always a
    >>> fixed four(4) char field.
    >>>
    >>> In vb.net and VB6 there is a function call FORMAT(56,"0000"), is there
    >>> anything equivalent to this is vbs...? or some sample code to create my
    >>> own Function...?
    >>>
    >>
    >> There must be better ways to do this, but here is a VBScript function
    >> that handles more numeric formats. This does not handle dates or negative
    >> numbers in parentheses:
    >> =========
    >> Wscript.Echo Format(12345.67, "##,#00.00")
    >> Wscript.Echo Format(12345.67, "$##,#00.00")
    >> Wscript.Echo Format(2345.67, "$##,#00.00")
    >> Wscript.Echo Format(345.67, "$##,#00.00")
    >> Wscript.Echo Format(45.67, "$##,#00.00")
    >> Wscript.Echo Format(5.67, "$##,#00.00")
    >> Wscript.Echo Format(2345.6, "$##,#00.00")
    >> Wscript.Echo Format(12345, "$##,#00.00")
    >> Wscript.Echo Format(-2345.67, "$##,#00.00")
    >> Wscript.Echo Format(-345.67, "$##,#00.00")
    >>
    >> Function Format(ByVal dblValue, ByVal strPattern)
    >> ' Function to format a number.
    >> Dim intPatternPoint, strPatternLeft, strPatternRight
    >> Dim strValueLeft, strValueRight
    >> Dim strValue, intValuePoint
    >> Dim j, k, blnLeading, strValueChar, strPatternChar
    >>
    >> strValue = CStr(CDbl(dblValue))
    >> intPatternPoint = InStr(strPattern, ".")
    >> If (intPatternPoint > 0) Then
    >> strPatternLeft = Left(strPattern, intPatternPoint - 1)
    >> strPatternRight = Mid(strPattern, intPatternPoint + 1)
    >> Else
    >> strPatternLeft = strPattern
    >> strPatternRight = ""
    >> End If
    >> intValuePoint = InStr(strValue, ".")
    >> If (intValuePoint > 0) Then
    >> strValueLeft = Left(strValue, intValuePoint - 1)
    >> strValueRight = Mid(strValue, intValuePoint + 1)
    >> Else
    >> strValueLeft = strValue
    >> strValueRight = ""
    >> End If
    >> strPatternLeft = StrReverse(strPatternLeft)
    >> strValueLeft = StrReverse(strValueLeft)
    >> Format = ""
    >> blnLeading = False
    >> k = 1
    >> For j = 1 To Len(strPatternLeft)
    >> If (k <= Len(strValueLeft)) Then
    >> strValueChar = Mid(strValueLeft, k, 1)
    >> Else
    >> blnLeading = True
    >> strValueChar = ""
    >> End If
    >> strPatternChar = Mid(strPatternLeft, j, 1)
    >> Select Case strPatternChar
    >> Case "0"
    >> If (blnLeading = True) Then
    >> Format = "0" & Format
    >> Else
    >> Format = strValueChar & Format
    >> End if
    >> k = k + 1
    >> Case "#"
    >> If (blnLeading = False) Then
    >> Format = strValueChar & Format
    >> End If
    >> k = k + 1
    >> Case "$"
    >> Format = strPatternChar & Format
    >> Case Else
    >> If (blnLeading = False) Then
    >> Format = strPatternChar & Format
    >> End If
    >> End Select
    >> Next
    >> If (intPatternPoint > 0) Then
    >> Format = Format & "."
    >> End If
    >> k = 1
    >> For j = 1 To Len(strPatternRight)
    >> If (k <= Len(strValueRight)) Then
    >> strValueChar = Mid(strValueRight, k, 1)
    >> Else
    >> strValueChar = ""
    >> End If
    >> strPatternChar = Mid(strPatternRight, j, 1)
    >> Select Case strPatternChar
    >> Case "0"
    >> If (strValueChar = "") Then
    >> Format = Format & "0"
    >> Else
    >> Format = Format & strValueChar
    >> End If
    >> k = k + 1
    >> Case "#"
    >> Format = Format & strValueChar
    >> k = k + 1
    >> Case Else
    >> Format = Format & strPatternChar
    >> End Select
    >> Next
    >> Format = Replace(Format, "-,", "-")
    >> Format = Replace(Format, "$-", "-$")
    >>
    >> End Function
    >> ======
    >> I appreciate the VB function more now.
    >>
    >> --
    >> Richard Mueller
    >> MVP Directory Services
    >> Hilltop Lab - http://www.rlmueller.net
    >> --
    >>
    >>
    >
    >

      My System SpecsSystem Spec

  10. #10


    Dr J R Stockton Guest

    Re: is there an equivalent Function to FORMAT(56,"0000")

    In microsoft.public.scripting.vbscript message <O58jYUyYJHA.1336@xxxxxx
    NGP02.phx.gbl>, Sat, 20 Dec 2008 23:32:09, Todd Vargo
    <tlvargo@xxxxxx> posted:

    >Function Pad(number)
    > Pad = Right("0000" & number, 4)
    >End Function

    Function Pad(number)
    Pad = Right(1e4 + number, 4)
    End Function

    is shorter and may be faster.


    It's not clear to me why the OP is, as he says, incrementing a STRING
    variable. Starting with "0000", though, one should always have four
    characters if he means what he says.

    The OP is probably safe in assuming that his variable will always
    represent a non-negative integer. But he should, when choosing a
    padding method, consider what should happen if the integer can ever
    exceed the expected upper 4-digit limit. Returning an undetected wrong
    result should be considered unacceptable in any circumstances.

    --
    (c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@xxxxxx
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

      My System SpecsSystem Spec

is there an equivalent Function to FORMAT(56,"0000") problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Format CDRW Failure: "Windows was unable to complete the format" Escher Vista General 5 29 Dec 2008
Vista's equivalent of "My Network places"? Michael Moser Vista networking & sharing 2 31 Aug 2008
New Problem - "From" Address format in "Microsoft Communities" John Saunders [MVP] Live Mail 5 03 Apr 2008
powershell equivalent of "DIR \\server\path /A:D /S /B" Ben Christian PowerShell 14 10 Mar 2008
Is there an equivalent of the DOS shell "start /wait" in PowerShel Brillig PowerShell 6 20 Jan 2007