Option Explicit
Private Sub Command1_Click()
Me.Print "|" & Format$(Format$(1.5, "$##0.00"), "@@@@@@@") & "|"
Me.Print "|" & Format$(Format$(12.5, "$##0.00"), "@@@@@@@") & "|"
Me.Print "|" & Format$(Format$(123.5, "$##0.00"), "@@@@@@@") & "|"
End Sub
Private Sub Command2_Click()
Dim x As String
x = (Format$(123.5, "$##0.00"))
Me.Print "x" & x & "x"
RSet x = (Format$(1.5, "$##0.00"))
Me.Print "x" & x & "x"
End Sub
Private Sub Command3_Click()
Dim required As Integer
Dim a As Single
Dim b As Single
Dim num1$, num2$
required = 8 ' longest number expected
a = 1.23
b = 44.56
num1$ = Format$(a, "#0.00") ' this converts the number to a string
num2$ = Format$(b, "#0.00") ' with two decimal places and a leading zero
'Debug.Print num2$
If (required - Len(num1$)) > 0 Then
num1$ = Space$(required - Len(num1$)) & num1$
End If
If (required - Len(num2$)) > 0 Then
num2$ = Space$(required - Len(num2$)) & num2$
End If
' test output
Me.Print num1$
Me.Print num2$
End Sub
Private Sub Command4_Click()
Dim xstring As String
xstring = LPad(2.3, 2, 7)
Me.Print "K" & xstring & "K"
End Sub
Private Sub Form_Load()
Command1.Caption = "@"
Command1.Font.Size = 18
Command2.Caption = "Rset"
Command3.Caption = "Format$"
Command4.Caption = "VBPJ"
Me.Font.Name = "Courier New"
End Sub
Private Function LPad(ValIn As Variant, nDec As Integer, _
WidthOut As Integer) As String
'
' Formatting function left pads with spaces, using specified
' number of decimal digits.
'
If IsNumeric(ValIn) Then
If nDec > 0 Then
LPad = Right$(Space$(WidthOut) & _
Format$(ValIn, "0." & String$(nDec, "0")), _
WidthOut)
Else
LPad = Right$(Space$(WidthOut) & Format$(ValIn, "0"), WidthOut)
End If
Else
LPad = Right$(Space$(WidthOut) & ValIn, WidthOut)
End If
End Function