Q(uick)BASIC Function: STR$

Quick View

STR$

A string function that returns a string representation of the value of a numeric expression

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • STR$(numeric-expression)
  • VAL(stringexpression$)
Description/Parameter(s)
numeric-expression Any numeric expression.
stringexpression$ A string representation of a number.
Example
PRINT "Decimal 65 is represented in hexadecimal as "; PRINT "&H" + LTRIM$(STR$(41)) PRINT VAL(RIGHT$("Microsoft 1990", 4))
Syntax
  • STR$(numeric-expression)
Description/Parameter(s)

If numeric-expression is positive, the string returned by the STR$ function contains a leading blank.

The VAL function complements STR$.

Example

The following example uses the STR$ function to convert a number to its string representation and strips out the leading and trailing blanks that BASIC ordinarily prints with numeric output.

'*** STR$ function programming example *** CLS ' clear the screen PRINT "Enter 0 to end." DO INPUT "Find cosine of: ",Num IF Num = 0 THEN EXIT DO X$ = STR$(Num) NumRemBlanks$ = LTRIM$(RTRIM$(X$)) PRINT "COS(" NumRemBlanks$ ") = " COS(Num) LOOP

Sample Output:

Enter 0 to end. Find cosine of: 3.1 COS(3.1) = -.9991351 Find cosine of: 0

See also:

Syntax
  • STR$(numeric-expression)
Description/Parameter(s)

STR$ returns a string representation of the value of numeric-expression.

Usage Notes

  • If numeric-expression is positive, the string returned by the STR$ function contains a leading blank.
  • The VAL function complements STR$.
Example

This example uses the STR$ function to convert a number to its string representation and the LTRIM$ and RTRIM$ functions to strip out the leading and trailing blanks that BASIC ordinarily prints with numeric output.

CLS 'Clear the screen. PRINT "Enter 0 to end." DO INPUT "Find cosine of: ", Num IF Num = 0 THEN EXIT DO X$ = STR$(Num) NumRemBlanks$ = LTRIM$(RTRIM$(X$)) PRINT "COS("; NumRemBlanks$; ") = "; COS(Num) LOOP

Sample Output:

Enter 0 to end. Find cosine of: 3.1 COS(3.1) = -.9991351 Find cosine of: 0

See also: