Microsoft KB Archive/95945

= How to Right Justify Numbers Using Format$ =

Article ID: 95945

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q95945



SUMMARY
NOTE: The technique described in this article works only with monospace fonts like Courier New, not proportional fonts like Times New Roman.

Use the following two-step process to right justify numbers in a string by using the format$ function:


 * 1) Format the number into a string by using the usual numeric conversion characters (0 # . ,).
 * 2) Format the resulting string by using a format string consisting of a number of @ characters equal to the length of the format string used in step 1.

The following example Sub procedure formats several numbers using the seven character formats $##0.00 and @@@@@@@: Sub Form_Click Print &quot;|&quot; + Format$(Format$(1.5, &quot;$##0.00&quot;), &quot;@@@@@@@&quot;) + &quot;|&quot; Print &quot;|&quot; + Format$(Format$(12.5, &quot;$##0.00&quot;), &quot;@@@@@@@&quot;) + &quot;|&quot; Print &quot;|&quot; + Format$(Format$(123.5, &quot;$##0.00&quot;), &quot;@@@@@@@&quot;) + &quot;|&quot; End Sub Here is the output:   |  $1.50| | $12.50|  |$123.50|



MORE INFORMATION
You can automatically generate the @ format string by using Len and String$ as in this example: Function rFormat (value As Variant, fmt As String) As Variant rFormat = Format(Format(value, fmt), String$(Len(fmt), &quot;@&quot;)) End Function

Additional query words: 2.00 3.00 align alignment right-justify

Keywords: KB95945

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.