Method

CStr


Description

Use to convert the passed data type to a String. Numbers are formatted using the system settings for numerical display.

Usage

result = CStr(Data)

Part

Type

Description

result

String

The string representation of the value passed.

data

Variant

The variable that will be represented as a string.

Notes

You can pass a class instance as long as the class implements the Operator Convert method that returns a String.

For real numbers, CStr returns 7 significant digits. When you pass a real number to CStr, the string it returns uses the decimal separator the user specified in system preferences. However, in most cases you will want to use the Format function instead for more precise control of the formatting. Use the Str function if you want to ensure US/English notation.

If you pass a Boolean, CStr will return either the string "True" or "False". If you pass a Color, it will return the hex representation of the color as a String. If you pass a DateTime, it will return the value of the date in SQL Date-time format.

Sample code

This code returns the string representation of two numbers and a Boolean.

Var s As String
Var d As New DateTime = DateTime.Now
s = CStr(1) // returns "1", as a string
s = CStr(d.Day) // returns the day number as a string
s = CStr(True) // returns "True"

This code returns the approximation to Pi as a String to 7 significant digits.

Var s As String
Const kPi = 3.14159265358979323846264338327950
s = CStr(kPi) // returns "3.141593"

Compatibility

All project types on all supported operating systems.

See also

Format, String.ToDouble, Str, Val functions; Boolean, Color, DateTime, String, Variant datatypes