ID Number: Q79094
1.00
WINDOWS
docerr
Summary:
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. This behavior is by
design.
The pound sign (#) place holder is handled differently between the
Visual Basic Format$ function and the Print Using statement found in
other Basic products. In the case of 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 using the pound sign as place
holders for the number. Note that Visual Basic does not support the
Print Using statement. Additional code is needed to right justify a
string using the Format$ function. An example is given further below.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows.
More Information:
Page 121 of the "Microsoft Visual Basic: Language Reference" for
version 1.0 regarding the Format$ function is unclear on how the pound
sign is handled. When there is no numeric digit to fill the pound sign
place holder, the manual does not clarify whether the pound sign is
replaced by a space or truncated. The documentation should be changed
to 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 "##0.00"; myvar
The above example will cause 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.
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 , " ##.## ")
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 will produce an output of right aligned
numbers in QuickBasic version 4.5.
a = 1.23
b = 44.56
Print Using "##.##"; a
Print Using "##.##"; b
The following code sample will produce an output of left aligned
numbers in Visual Basic:
Sub Form_Click ()
a = 1.23
b = 44.56
num1$ = Format$(a, "##.##")
num2$ = Format$(b, "##.##")
Print num1$
Print num2$
End Sub
Click on the form to print the numbers. These numbers will be left
aligned, instead of right aligned as may be desired.
A workaround is to 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 in order to right align the
result.
Workaround
----------
Sub Form_Click ()
desired = 5 'longest number expected
a = 1.23
b = 44.56
FontName = "Courier" 'Select a fixed-spaced font
num1$ = Format$(a, "#0.00") 'This converts number to a string
num2$ = Format$(b, "#0.00") '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
Additional reference words: 1.00 4.50