Microsoft KB Archive/79094

= PRB: Format$ Using # for Digit Affects Right Alignment =

Article ID: 79094

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
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q79094



SYMPTOMS
The pound (#) sign does not serve as a place holder for blank spaces when used with the Format$ function to reformat numbers as strings. If a pound sign place holder is not filled by a digit, Format$ truncates that digit position and will not replace that position with a space. This may be undesirable behavior if you are attempting to right justify the numeric digits within the string.



CAUSE
Visual Basic Format$ function handles the pound sign (#) place holder differently from the way the it's handled in the Print Using statement found in other Basic products. In the Print Using statement, a pound sign place holder is replaced by a space when no numeric digit occupies that position. By using the Print Using statement, you can right justify a formatted numeric string by using the pound sign as place holders for the number. Visual Basic does not support the Print Using statement, so you need to use additional code to right justify a string using the Visual Basic Format$ function. An example is given below.



WORKAROUND
To work around the problem, use a monospaced font, such as Courier, and use the Len function to determine how many spaces need to be added to the left of the string representation of the number to right justify the result. Here is the example code: Sub Form_Click desired = 5  'longest number expected a = 1.23 b = 44.56 FontName = &quot;Courier&quot;  'Select a fixed-spaced font num1$ = Format$(a, &quot;#0.00&quot;) 'This converts number to a string num2$ = Format$(b, &quot;#0.00&quot;) '2 decimal places and a leading 0 If (desired - Len(num1$)) > 0 Then num1$ = Space$(desired - Len(num1$)) + num1$ End If  If (desired - Len(num2$)) > 0 Then num2$ = Space$(desired - Len(num2$)) + num1$ End If  Print num1$ Print num2$ End Sub



STATUS
This behavior is by design.



MORE INFORMATION
Page 121 of the &quot;Microsoft Visual Basic: Language Reference&quot; for version 1.0 regarding the Format$ function doesn't specify how the pound sign is handled. When there is no numeric digit to fill the pound sign place holder, the manual does not specify whether the pound sign is replaced by a space or truncated. The documentation should reflect how the pound sign is handled by the Format$ function.

The Print Using statement supported in other Basic products allows the use of the pound sign as a place holder for leading or trailing spaces, as follows: Print Using &quot;##0.00&quot;; myvar The above example causes two leading spaces to be added to the resulting string representation of the variable myvar when the value of myvar is printed to the screen.

However, when used with the Visual Basic Format$ function, the same pound sign format switch (#) does not work as a placeholder for spaces: mystring$ = Format$(myvar, &quot; ##.## &quot;) The Visual Basic Format$ function yields a formatted string representation of myvar with no leading spaces. This may not be the result you expected (for example, when myvar = 1.23). You may have expected the formatted result to have one leading space allowing you to right justify the number, but no leading space is added.

The following code sample produces an output of right justified numbers in Microsoft QuickBasic version 4.5: a = 1.23 b = 44.56 Print Using &quot;##.##&quot;; a  Print Using &quot;##.##&quot;; b The following code sample produce an output of left justified numbers in Visual Basic: Sub Form_Click a = 1.23 b = 44.56 num1$ = Format$(a, &quot;##.##&quot;) num2$ = Format$(b, &quot;##.##&quot;) Print num1$ Print num2$ End Sub Click the form to print the numbers. These numbers will be left justified, instead of right justified as may be desired.

Additional query words: 2.00 3.00 4.50 alignment aligned align right-justify docerr

Keywords: kbprb KB79094

-

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

© Microsoft Corporation. All rights reserved.