Contenido

# Función Str

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

Los números no negativos van precedidos de un espacio en blanco. Los números negativos van precedidos de un signo de resta.

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.

Las fechas se convierten en cadenas dependientes de la configuración regional.

### Sintaxis:

        Str (Value As Variant)

### Return type:

String

### Parámetros:

Value: Any value to be converted into a string.

### Códigos de error:

5 Llamada a procedimiento no válida

### Ejemplo:

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

Temas relacionados

Función Val

¡Necesitamos su ayuda!