Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      Abs
      And
      Append
      Asc
      Atn
      Auto
      Backup
      Bank
      Begin
      Bend
      Bload
      Boot
      Box
      Bsave
      Bump
      Catalog
      Char
      Chr
      Circle
      Close
      Clr
      Cmd
      Collect
      Collision
      Color
      Concat
      Cont
      Copy
      Cos
      Data
      Dclear
      Dclose
      Dec
      Def
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      Instr
      Int
      Joy
      Key
      Left
      Len
      Let
      List
      Load
      Locate
      Log
      Loop
      Mid
      Monitor
      Movspr
      New
      Next
      Not
      Off
      On
      Open
      Or
      Paint
      Peek
      Pen
      Pi
      Play
      Pointer
      Poke
      Pos
      Pot
      Print
      Print num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      Rnd
      Rreg
      Rspcolor
      Rsppos
      Rsprite
      Run
      Rwindow
      Save
      Scale
      Scnclr
      Scratch
      Sgn
      Sin
      Sleep
      Slow
      Sound
      Spc
      Sprcolor
      Sprdef
      Sprite
      Sprsav
      Sqr
      Sshape
      St
      Stash
      Step
      Stop
      Str
      Swap
      Sys
      Tab
      Tan
      Tempo
      Then
      Ti
      Ti string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
STR$ST{Shift+R}C41.0+Function

 Syntax 
STR$ number )
 
ParametersTypeLegal Value(s)Default ValueNote(s)
numberFloating-point or Integer
any
 
ReturnsTypeValue(s)Note(s)
decStringString
sign character, 1 to 9 digits, possible exponent
positive numbers have a space for the sign character
if ABS(number) >= 1.0e9 or less than .01, scientific format is generated
 
 
 Purpose 
Return a string containing a decimal represention of the given number.

 
 Remarks 
The STR$ function generates a (normal, decimal) string representation of a given number, much like you would get with PRINT.  Unlike the similar HEX$ function, this one will accept any number.  The first character will be a space if the number is zero or positive; a minus/hyphen if negative.  Up to nine digits may follow.  If the number is a mathematical integer (not neccessarily a BASIC integer-type) no decimal point or fraction will be generated.  If the number is smaller than 1/100 (0.01) or larger than or equal to 1 billion (1.0e9), then scientific notion will used (i.e., a trailing E, a + or -, and 2-digit exponent).
 
Because BASIC will automatically translate numeric values into a string when using CMD, PRINT, or PRINT#, there is seldom reason to use this, unless you want to process the actual characters that make up the string representation of the number.  (Such as remove the leading space of positive numbers.)  Even in those cases, the USING clause to PRINT / PRINT# is often easier to use (if available in your version of BASIC).  STR$ is handy when a BASIC command/function/statement does not allow a parameter to be numeric, with CHAR being a good example, or when building a string to be sent to a device (typically a command or perhaps some file data).
 
TYPE MISMATCH ERROR is generated with a string expression or variable given for the number (the DEC function may help).  If number is omitted or an invalid expression, SYNTAX ERROR occurs.
 
Because the first character will be a space if the number is non-negative, you may want to remove it.  Here is one way to remove it (it will cut-off the sign if negative):
 MID$(STR$(number),2)
  
Here is a slightly more complex version, but it will not cut-off the sign of a negative number:
 MID$(STR$(number), 2+(number<0) )
 
One important difference from the way STR$ generates a string representation of a number, as compared to the way PRINT and PRINT# work is that there is no trailing "space".  For example, PRINT 7;"X" would generate " 7 X", but PRINT STR$(7);"X" would generate " 7X".
 
Examples:
PRINT STR$("65")

?TYPE MISMATCH ERROR 
READY.
PRINT STR$(65)
 65
 
READY.
PRINT STR$(0.9)
 .9

READY.
PRINT STR$(0.009)
 9E-03

READY.
PRINT STR$(1.5E8)
 150000000

READY.
PRINT STR$(1.5E9)
 1.5E+09

READY.
  
 Compare With 
 
 Contrast With 
ASC, DECINSTRVAL 
 
 See Also 

© H2Obsession, 2014