How to Right Justify a String in Basic (84065)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • 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$

MORE INFORMATION

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$ = ""
    EXIT FUNCTION
 END IF
 TempJustify$ = STRING$(NumPos, "0")
 Justify$ = LEFT$(TempJustify$, LEN(TempJustify$)
 LEN(Num$)) + Num$

END FUNCTION
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB84065