Str Function

Purpose

Converts a numeric expression into a string.

Syntax

$ = Str[$](x [,m, n])

x:aexp

m, n:integer expression

Description

Str$(x, m) converts x into a string of m length. If m is greater than the number of characters needed to represent x, the string is padded with leading spaces. If m is smaller than the number of characters needed to represent x, the string is truncated from the right.

Str$(x, m, n) converts x into a string of m length with n decimal places. The last decimal place is rounded off. Out of the total length m, n+1 places are reserved (n places for the decimal part and one place for the decimal point).

With positive expressions Str adds a space in front of the number. With negative values a minus is added. The additional space in front of positive values is a VB quirk and is mimicked by GFA-BASIC 32. To prevent the space for positive numbers use Mode StrSpace 0.

Example

Debug.Show

Trace Str$(3 * 4 + 2)     //  Prints " 14"

Local a$ = Str$(3 * 4 + 2)

Mode StrSpace 0

Trace a$                  //  Prints "14"

Trace Str$(123.456, 7)    //  Prints 123.456

Trace Str$(123.456, 9)    //  Prints   123.456

Trace Str$(123.456, 5)    //  Prints 123.4

Trace Str$(123.456, 7, 3) //  Prints 123.456

Trace Str$(123.456, 7, 5) //  Prints 3.45600

Trace Str$(123.456, 7, 2) //  Prints 123.46

Trace Str$(123.456, 9, 3) //  Prints 123.456

Remarks

The Print [#] commands use the Str() function internally to convert a numeric expression to a printable string. Therefore, Print adds a space in front of a positive value as well. The Mode StrSpace 0 prevents the adding of a space.

Without the optional $ character the function still returns a String data type and not a Variant.

See Also

String, Dec$(), Hex$(), Oct$(), CStr, Using, Mode, Format, sprintf

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}