大网数据广告活动
Web服务器与PHP

Format strings to Right-Justify when an app calls printing

· 52CE 我爱测

This article describes how to format strings to Right-Justify when an application calls printing.

This article introduces how to format strings to Right-Justify when an application calls printing.

Original product version:   Visual Basic
Original KB number:   217012

Summary

There are several different ways to right-justify strings using the Format function:

  • Use the @ character.

  • Use the RSet function.

  • Use workarounds with the Format$ function.

Using the @ character

Note: This technique is only effective with monospace fonts, such as Courier New.

  1. Format the number into a string with numeric conversion characters, for example, $##0.00.

  2. Format the resulting string with a format string consisting of a number of @ characters equal in length to the desired format, for example, @@@@@@@.

The following code sample formats several numbers using seven @ characters and a seven character format, $##0.00.

 Print "|" & Format$(Format$(1.5, "$##0.00"), "@@@@@@@") & "|"
 Print "|" & Format$(Format$(12.5, "$##0.00"), "@@@@@@@") & "|"
 Print "|" & Format$(Format$(123.5, "$##0.00"), "@@@@@@@") & "|"

The output is;

|  $1.50|
| $12.50|
|$123.50|

Using the RSet function

When used in conjunction with RSet, the format function works on fixed-length strings. The following code sample illustrates the use of RSet:

 x = (Format$(123.5, "$##0.00"))
 Print "x" & x & "x"
 RSet x = (Format$(1.5, "$##0.00"))
 Print "x" & x & "x"

The output is:

x$123.50x 
x $1.50x

Workarounds using the Format$ function

Note: These techniques are only effective with monospace fonts, such as Courier New.

The Format$ function does not right-justify strings when used with the # symbol. The first code sample uses the Len function to determine how many spaces need to be added to the left of the string representing the number, in order to right justify the string:

 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$

The output is:

 1.23
44.56

The second Format$ sample is reprinted with the permission of its author, Karl Peterson. His LPad function uses the Right$ function:

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

Step by step sample

  1. Start a new Visual Basic Standard EXE project. Form1 is created by default.

  2. Add four CommandButton controls to Form1. Position them to the far right of the form window.

  3. Add the following code to the General Declarations section of 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
    
  4. Run the program, click the command buttons, and observe the results.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 12/10/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。