Condividi tramite


Formattare le stringhe a Right-Justify quando un'applicazione chiama la stampa

Questo articolo illustra come formattare le stringhe in Right-Justify quando un'applicazione chiama la stampa.

Versione originale del prodotto: Visual Basic
Numero KB originale: 217012

Riepilogo

Esistono diversi modi per giustificare le stringhe a destra usando la funzione Format:

  • Usare il @ carattere .

  • Usare la funzione RSet.

  • Usare le soluzioni alternative con la Format$ funzione .

Utilizzo del carattere @

Note

Questa tecnica è efficace solo con monospace i tipi di carattere, ad esempio Courier New.

  1. Formattare il numero in una stringa con caratteri di conversione numerici, ad esempio $###0.00.

  2. Formattare la stringa risultante con una stringa di formato costituita da un numero di caratteri @ di lunghezza uguale al formato desiderato, ad esempio @@@@@@@.

L'esempio di codice seguente formatta diversi numeri usando sette @ caratteri e un formato di sette caratteri, $##0.00.

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

L'output è;

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

Uso della funzione RSet

Se usata in combinazione con RSet, la funzione di formato funziona su stringhe a lunghezza fissa. L'esempio di codice seguente illustra l'uso di RSet:

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

L'output è il seguente:

x$123.50x 
x $1.50x

Soluzioni alternative che usano la funzione Format$

Note

Queste tecniche sono efficaci solo con monospace i tipi di carattere, ad esempio Courier New.

La funzione Format$ non giustifica le stringhe corrette quando viene usato con il simbolo #. Il primo esempio di codice usa la funzione Len per determinare il numero di spazi da aggiungere a sinistra della stringa che rappresenta il numero, per giustificare correttamente la stringa:

 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$

L'output è il seguente:

 1.23
44.56

Il secondo esempio Format$ viene ristampato con l'autorizzazione del suo autore, Karl Peterson. La funzione LPad usa la funzione 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

Esempio dettagliato

  1. Avviare un nuovo progetto EXE Standard di Visual Basic. Form1 viene creato per impostazione predefinita.

  2. Aggiungere quattro controlli CommandButton a Form1. Posizionarli all'estrema destra della finestra del modulo.

  3. Aggiungere il codice seguente alla sezione Dichiarazioni generali di 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. Eseguire il programma, fare clic sui pulsanti di comando e osservare i risultati.