"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
--