Microsoft KB Archive/84065

= How to Right Justify a String in Basic =

Article ID: 84065

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q84065



SUMMARY
There is no command in either QuickBasic or Basic PDS for simple justification of strings.

The following function returns a string that is right justified. This string could be used for printing or other tasks.

Note: This function is written to handle numbers of SINGLE precision type. A change in the FUNCTION statement and DECLARE statement is all that is required to make it work with different data types. This function could be modified to work with left justification or centering using the same idea used for the right justification.

For information on how to convert a number in scientific notation to standard notation before or while performing the justification function, query for the following words in the Microsoft Knowledge Base:

scientific and notation and REALSTRING$



Example
DECLARE FUNCTION Justify$ (x AS SINGLE, NumPos AS INTEGER)

Number = 2557.631 JustString$ = Justify(Number, 30) CLS PRINT JustString$

FUNCTION Justify$ (Number AS SINGLE, NumPos AS INTEGER) ' This function takes two parameters: ' '  1. Number is the value to be justified. Note it is a '     SINGLE. This would need to be changed if you wanted to '     justify another type of variable.

'  2. NumPos is the number of positions desired in the '     return string ' '  If the length of the number is greater than the number '  of positions desired, an empty string is returned.

Num$ = LTRIM$(STR$(Number)) IF LEN(Num$) > NumPos THEN Justify$ = &quot;&quot; EXIT FUNCTION END IF TempJustify$ = STRING$(NumPos, &quot;0&quot;) Justify$ = LEFT$(TempJustify$, LEN(TempJustify$) LEN(Num$)) + Num$

END FUNCTION

Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB84065

-

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

© Microsoft Corporation. All rights reserved.