本文介绍如何在应用程序调用打印时将字符串格式设置为 Right-Justify。
原始产品版本: Visual Basic
原始 KB 数: 217012
总结
使用 Format 函数对字符串进行右对齐的方法有多种:
使用
@
字符。使用
RSet
函数。将
Format$
解决方法与函数一起使用。
使用 @ 字符
注意
此方法仅对 monospace
字体有效,例如 Courier New
。
将数字格式化为包含数字转换字符的字符串,例如 $##0.00。
使用格式字符串格式化生成的字符串,其长度等于所需格式的 @ 字符数,例如@@@@@@@。
下面的代码示例使用七个 @ 字符和七个字符格式 $##0.00 设置多个数字的格式。
Print "|" & Format$(Format$(1.5, "$##0.00"), "@@@@@@@") & "|"
Print "|" & Format$(Format$(12.5, "$##0.00"), "@@@@@@@") & "|"
Print "|" & Format$(Format$(123.5, "$##0.00"), "@@@@@@@") & "|"
输出为;
| $1.50|
| $12.50|
|$123.50|
使用 RSet 函数
与 RSet
结合使用时,格式函数适用于固定长度字符串。 下面的代码示例说明了如何使用 RSet
:
x = (Format$(123.5, "$##0.00"))
Print "x" & x & "x"
RSet x = (Format$(1.5, "$##0.00"))
Print "x" & x & "x"
输出为:
x$123.50x
x $1.50x
使用 Format$ 函数的解决方法
注意
这些技术仅对 monospace
字体有效,例如 Courier New
。
与 # 符号一起使用时,Format$ 函数不右对齐字符串。 第一个代码示例使用 Len 函数来确定需要向表示数字的字符串左侧添加多少个空格,以便右对齐字符串:
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 2 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
Print num1$
Print num2$
输出为:
1.23
44.56
第二个 Format$ 样本由其作者卡尔·彼得森的许可重新打印。 他的 LPad 函数使用 Right$ 函数:
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
分步示例
启动新的 Visual Basic Standard EXE 项目。 Form1 默认创建。
向 Form1 添加四个 CommandButton 控件。 将它们定位到窗体窗口最右侧。
将以下代码添加到 Form1 的“常规声明”部分:
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
运行程序,单击命令按钮并观察结果。