STR (string)
| Keyword | Token | Version | Category(s) |
|---|---|---|---|
| STR$ | $C4 | 1.0 | Function |
Syntax
STR$( arg )
Where arg is any numeric literal, variable or expression. Note the token is 4 characters; it includes the dollar sign ($).
Purpose
Convert a number to its (decimal) string representation.
Return Value
A string of characters representing the number arg.
Notes
This function is the inverse of VAL(). STR$() generates a string of decimal digits from a number; see HEX$() for generating hexadecimal digits.
The first character of the returned string will be a dash (-) if arg is negative, otherwise the first character is a space.
If arg has more than 9 integer digits (absolute value greater than or equal to 1e9) the generated string will be in scientific notation, otherwise it will be in simple notation.
PRINT STR$(123456789) 123456789 PRINT STR$(1234567890) 1.23456789E+09 PRINT STR$(-123456789) -123456789 PRINT STR$(1E+02) 100
A pure fraction (arg greater than -1 and less than +1) will not have a leading zero before the decimal point. If the number has more than one leading zero after the decimal point (absolute value of arg less than 0.01), the number will be formatted in scientific notation.
PRINT STR$(0.1) .1 PRINT STR$(0.01) .01 PRINT STR$(0.001) 1E-03
Because BASIC can print numeric values as easily as string values (both to the screen or a file), the STR$() function is mainly only used to format a number when the BASIC numeric output is not satisfactory. One problem, especially when printing to a file, is that numeric values print with a trailing cursor right (character $1D = CHR$(29)). Converting the number to a string with this function eliminates that. The programmer may also want to remove the leading space for positive numbers, replace the space with plus (+), add leading/trailing zeros, insert seperators, or append monetary characters. However for newer versions of BASIC, the USING preposition is available for that.
Another use is to combine a program-generated number with some existing text, such as adding page numbers to a text file or appending a sequence number to a filename.
Compare with HEX$()
Contrast with VAL(), DEC()
See also MID$()
This page is a member of Commodore BASIC