Contents

# Str Function

The Str function converts the contents of variables into a string. It handles numeric values, dates, strings and currency values.

Positive numbers are preceded by a blank space. Negative numbers are preceded by a minus sign.

For numeric values the string returned by the Str function is locale-independent. Hence the dot is used as the decimal separator when needed.

If a string is passed as argument, it is returned without any changes.

Dates are converted into locale-dependent strings.

### Syntax:

        Str (Value As Variant)

### Return type:

String

### Parameters:

Value: Any value to be converted into a string.

### Error codes:

5 Invalid procedure call

### Example:

Below are some numeric examples using the Str function.

        Sub ExampleStr_1
            ' Note the blank space at the beginning of the returned strings
            MsgBox Str(10) ' " 10"
            MsgBox Str(10.5) ' " 10.5"
            MsgBox Str(-12345 + 1.3) ' " -12346.3"
            MsgBox Str(10000 / 3) '  " 3333.33333333333"
            ' Strings passed as arguments are left unchanged
            MsgBox Str("A123") ' "A123"
        End Sub

Use the LTrim function to remove the blank space at the beginning of the returned string.

        Sub ExampleStr_2
            MsgBox Str(10.5) ' " 10.5"
            MsgBox LTrim(Str(10.5)) ' "10.5"
        End Sub

The Str function can also handle Date variables.

        Sub ExampleStr_3
            Dim aDate as Date, aTime as Date
            aDate = DateSerial(2021, 12, 20)
            aTime = TimeSerial(10, 20, 45)
            Print Str(aDate) ' "12/20/2021"
            Print Str(aTime) ' "10:20:45"
        End sub

Related Topics

Val Function