PRB: Format$ Using # for Digit Affects Right AlignmentLast reviewed: June 21, 1995Article ID: Q79094 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SYMPTOMSThe 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.
CAUSEVisual 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.
WORKAROUNDTo 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 = "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 STATUSThis behavior is by design.
MORE INFORMATIONPage 121 of the "Microsoft Visual Basic: Language Reference" 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 "##0.00"; myvarThe 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 , " ##.## ")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 "##.##"; a Print Using "##.##"; bThe 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, "##.##") num2$ = Format$(b, "##.##") Print num1$ Print num2$ End SubClick the form to print the numbers. These numbers will be left justified, instead of right justified as may be desired.
|
Additional reference words: 1.00 2.00 3.00 4.50 alignment aligned align
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |