In many programs, conversions between different data types are necessary if, for example, values from a TextBox are to be processed as numbers or as dates according to the type. For all conversion functions, note the data type of the arguments and the data type of the function value as well as the notes on localization.
Function | Description |
---|---|
CBool(Expr) AS Boolean | Converts an expression to a true or false value. An expression is guaranteed to be incorrect if a true value is wrong or a number is 0 or the string length is 0 or a NULL date exists or a zero object. |
CByte(Expr AS Variant) AS Byte | Converts an expression into a byte. The expression is first converted to an integer value. If the integer value exceeds the byte range[0… 255] then it is transformed (modulo 256) into this range. |
CDate(Expr AS Variant) AS Date | Converts an expression (number of data type integer or float) into a date/time string. CDate does not use the current localization! |
CFloat(expr AS Variant) AS Float | ↓ |
CFlt(Expr AS Variant) AS Float | Converts an expression to a floating point number. Careful! This function does not use the selected localization. A dot must be set as a decimal separator! |
CInt(Expr AS Variant) AS Integer | ↓ |
CInteger(Expr AS Variant) AS Integer | Converts an expression into a number of the data type integer. |
CLong(Expr AS Variant) AS Long | Converts an expression to a number of the data type Long. An overrange is not detected. |
CShort (Expr AS Variant) AS Short | Converts an expression into a number of the data type Short. The expression is first converted to an integer value. If the integer value exceeds the short range[-32.768… 32.767], then it is transformed into this range. The least significant 16 bits are extracted from Expr and interpreted as an integer in the sense of the two's complement. |
CSingle(Expr AS Variant) AS Single | Converts an expression into a number of the data type Single. This function does not use the set localization! |
CStr(Expr AS Variant) AS String | ↓ |
CString(Expr AS Variant) AS String | Converts an expression into a string. This function does not use the set localization! |
CVar(expr) AS Variant | ↓ |
CVariant(Expr) AS Variant | Converts an expression to a variant. This is useful if the result of a function depends on the data type of the arguments. |
Str$(expr) AS String | ↓ |
Str(Expr) AS String | Converts a number or date into a string. This function observes the localization settings! |
DConv$(string AS string) AS String | ↓ |
DConv(string AS string) AS String | Converts a string from the system character set to UTF-8, the desktop character set. |
SConv$(string AS string) | ↓ |
SConv(string AS string) | Converts a string from the desktop character set (which should be UTF-8) to the system character set. |
Val(string) | Converts a string into a number or date. This function takes into account the selected localization! |
Table 9.10.1.1: Overview of the conversion functions
The conversion algorithm for the Val () function is the following:
ConvertedString = Conv$(string AS string, source character set AS string, target character set AS string) ConvertedString = Conv(string AS string, source character set AS string, target character set AS string)
Converts a string from one character set to another. A character set is represented by a string such as ASCII, ISO-8859-1, or UTF-8.
Dim sNumber As Single Print CBool(0), CBool(1), CBool("Gambas"), CBool(""), CBool(Zero), CBool(Date(2000, 2, 29)) Print CByte("23"), CByte("257"), CByte(True) Print "CDate(2488913) = "; CDate(2488913) Print "UnixTimeStamp = "; DateDiff(CDate("1/1/1970"), Now, gb.Second) Try CDate("12.2. 2014 12:45:00") If Error.Code = 6 Then Print "The expression ''; "12.2. 2014 12:45:00"; "' cannot be converted!" Print "Date: ", Format(CDate(Val("1.9.2012")), "d. mmmm yyyy") Print "Days since 1.1. 1970 = "; DateDiff(CDate("1/1/1970"), Now, gb.Day) ' 16224 Print CFloat("+3.1416"), CFloat(Now()) Print CInt("17"), CInt(True), CInt(Pi(1 / 7)), CInt(3.8), CInt(-7.998), CInt(-7.1), CInt(Now) Print "CLong(5 ^ (2 ^ 2)) = "; CLong(5 ^ (2 ^ 2)) Print "CLong(2^62) = "; CLong(2 ^ 62) Print CShort(20 < 6), CShort(False), CShort(True), CShort(DateDiff(CDate("1/1/1970"), Now, gb.Day)) Try sNumber = CSingle(DateDiff(CDate("1/1/1970"), Now(), gb.Millisecond)) If Error Then Print "OVERVIEW" Print CSingle(Pi), CFloat(Pi), CSingle(Exp(11)), CSingle("+355.11") Print CStr(-99), CStr(Pi(Pi())), CStr(355 / 113), CStr(Now) Print "Object.Type(CVariant([2, 3, 5])) = "; Object.Type(CVariant([2, 3, 5])) Print "Object.Type(CVariant([\"2\",\"3\",\"5\"])) = "; Object.Type(CVariant(["2", "3", "5"])) Print "Object.Type([CVariant(\"a\"),\"b\",\"c\"]) = "; Object.Type([CVariant("a"), "b", "c"]) Print "Val(\"True\") = "; Val("True"), "Val(\"False\") = "; Val("False") Print Format(Val("12.11.2013"), "d. mmmm yyyy") Print "Val(\"123,456\") = "; Val("123,456"); " (Number with comma as decimal separator for DE.de)" Print "Val(\"123.456\") = "; Val("123.456"); " (Point is interpreted as a thousands separator!)" If Val("123.66") = Zero Then Print "Val(\"123.66\") = ZERO"; " (String cannot be interpreted as a number.)" Print Conv("Trouble on Oedipus Street", System.Charset, "ISO 8859-15") Print "Desktop-Characterset = "; Desktop.Charset Print "System-Character set = "; System.Charset
False True True False False True 23 1 255 CDate(2488913) = 30.05.2014 23:00:00 UnixTimeStamp = 1401992840 The expression' 12.2.2014 12:45:00' cannot be converted! Date: 1. September 2012 Days since 1.1. 1970 = 16226 3,1416 2488918,81065353 17 -1 0 3 -7 -7 2488918 CLong(5 ^ (2 ^ 2)) = 625 CLong(2^62) = 4611686018427387904 0 0 -1 16226 Overflow 3,1415927 3,14159265358979 59874,140625 355,1099854 -99 9.86960440108936 3.14159292035398 06/05/2014 18:27:20.466 Object.Type(CVariant([2, 3, 5])) = Integer[] Object.Type(CVariant(["2","3","5"])) = String[] Object.Type([CVariant("a"),"b","c"]) = Variant[] Val("True") = True Val("False") = False 12. November 2013 Val("123,456") = 123,456 (Number with comma as decimal separator for DE.de) Val("123.456") = 123456 (Point is interpreted as a thousands separator!) Val("123.66") = ZERO (String cannot be interpreted as a number.) �rger in der �dipus-Stra�e Desktop-Character set = UTF-8 System-Character set = UTF-8