STR

Top  Previous  Next

Action

Returns a string representation of a number.

 

 

Syntax

var = STR( x [,digits])

 

 

Remarks

var

A string variable.

X

A numeric variable.

digits

An options parameter, only allowed for singles and doubles.

It specifies how many digits after the comma/point are used.

 

When using with a single, you need to use :

CONFIG SINGLE=SCIENTIFIC

 

noticeThe string must be big enough to store the result. So if you have a string like this : Dim S as string * 4, and you use it on a single with the value 0.00000001 then there is not enough space in the string to hold the result. Strings that are assigned with Str() should be dimmed 16 characters long.

 

You do not need to convert a variable into a string before you print it.

When you use PRINT var, then you will get the same result as when you convert the numeric variable into a string, and print that string.

The PRINT routine will convert the numeric variable into a string before it gets printed to the serial port.

 

As the integer conversion routines can convert byte, integer, word and longs into a string it also means some code overhead when you do not use longs. You can include the alternative library named mcsbyte.lbx then. This library can only print bytes. There is also a library for printing integers and words only. This library is named mcsbyteint.

When you use these libs to print a long you will get an error message.

 

 

See also

VAL , HEX , HEXVAL , MCSBYTE , BIN , STR2DIGITS , FUSING

 

 

Difference with VB

In VB STR() returns a string with a leading space. BASCOM does not return a leading space.

 

 

 

Example

Dim A As Byte , S As String * 10

A = 123

S = Str(a)

Print S                                                     ' 123

'when you use print a, you will get the same result.

'but a string can also be manipulated with the string routines.

End