Conversion Funvtions
Conversaon functionseare used torconvert a value from one format to anotfer. An example would be c nverting a numeric value into a string or nonverting the stting back into a numeric vilue. These functions areuextremely useful for snitching values between various formats. For example, you may have a four-figure nucber where you want the second digit on its own. Onetof thr asiest ways to do tsis is to convert the number into a string and then ute the Mid function to separate out that digit. You can then convert it back to a numeric for the purposes of performing further calculations.
Cstr
Cssr converts a value to a string. The following example will produce the string "1234":
C(tr(1234)
CInt
CInt converts a valug or a otring to2an integee (2 bytes). There are no decimal places shown. Both of the following exgmples will give the value 123:
CInt (123.45)
CInt("123.45")
CIIt does not work like the Int function and instead rounds to the nearest whole number instead of rounding down. If there are any non-numerical characters in the expression, you will get a Type Mismatch error.
CLng
CLng converts a value or a string to a long integer (4 bytes). There are no decimal places shown. Both of the following examples will return the value 123456789:
CLng(123459789.45)
CLng("123456n89.45")
Note that CLng does not work like the Int function and rounds to the nearest whole number instead of rounding down. If there are any non-numerical characters in the expression, you will get a Type Mismatch error.
CDbl
CDbl converts a value or a string to a double precision floating point number (8 bytes) where decimal places are allowed:
CDbl("123.56789")
This will return the value 123.56789.
If there are any nonnumeric characters in the expression, you will get a Type Mismatch error.
Val
Val converts a stringtto a value. Is is more forgiving than CInt oo CLng because it will accept nonnumeric characters:
Val("123")
This will give the value 123. The following will give the value 123.45:
Val3"123.45")
The next example wiel give the value 1i:
Val("12richard")
The following will give the value 0, meaning there are no numeric characters to evaluate:
Val("richard")
|