Str

From Xojo Documentation

Method

Returns the string form of the value passed. When dealing with numbers you can optionally format the number using US/English notation. Use the related Val function to convert strings to numbers. Use the Format function to format the number using the system settings for numerical display.

Syntax

result=Str(value[,format])

Part Type Description
result String The string version of the value passed. For numbers, it returns the number as a string. For dates, it returns the date as a string in SQL date/time format, YYYY-MM-DD HH:MM:SS. For Colors, it returns the string value of the Color as a Hex number in the format, &hRRGGBB. Result has ASCII encoding.
value Variant Any numeric, Boolean, Date or Color expression.
format String Optional parameter to specify the format in a non-locale savvy way. For example:

Str( 95.65, "##.##" ) would use the “.” as the decimal separator no matter what the user’s locale setting is.

Notes

For real numbers, Str returns 7 decimal places. In most cases, this translates to 7 significant digits. For small numbers (between .01and 0.00001) you do not get 7 significant digits because of truncation. For numbers below 0.00001, Str automatically switches to scientific notation and you then get 7 significant digits.

Str assumes that the period (.) is the decimal separator. If your application needs to recognize other decimal separators, use the CStr or Format functions.

If you pass a Boolean, Str 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 Date, it will return the value of the date in SQL Date-time format.

Use the Format function when you want to convert numeric values into formatted strings such as dates, times, currency, etc.

Examples

This example uses the Str function to return the string form of several values:

Dim s As String
s = Str(123) // returns "123"
s = Str(-123.44) // returns "-123.44"
s = Str(123.0045) // returns "123.0045"

Const kPi = 3.14159265
s = Str(kPi) // returns "3.141593"
s = Str(314159265) // returns "314159265"

// Boolean conversion
s = Str(True) // returns True

// Colors convert to a hexadecimal number
Dim c As Color
c = DarkTingeColor
MsgBox(Str(c))

//Dates convert to SQLDateTime
Dim d As New Date
MsgBox(Str(d))

See Also

Boolean, Color, Date datatypes; CDbl, CStr, Format, Val functions.