Numerical conversion functions
The numerical conversion functions
cDec,
cInt,
cLng etc. convert a string to the corresponding
datatype.
If the string starts or ends with a minus sign, the returned value is negative, too:
debug.print(cInt("42-"))
-42
debug.print(cDbl("-8.42"))
-8.42
cDec
Although VBA doesn't have a decimal
datatype, a string value (such as
"42.1234") can be converted to a decimal with
cDec.
cLng
option explicit
sub main()
' Spaces around the number are possible
debug.print cLng(" 123 " )
' Not possible, type missmatch error
' debug.print cLng(" 456 xyz")
' With the &h prefix, the following text is interpreted as
' a number's hexadecimal representation.
' The following prints 42.
debug.print cLng("&h2a" )
' 17.3 is rounded down to 17
debug.print cLng("17.3")
' 17.7 is rounded up to 18
debug.print cLng("17.7")
end sub
Misc
Although VBA doesn't have a decimal
datatype, a string value (such as
"42.1234") can be converted to a decimal with
cDec.
Arguably, the functions
asc,
hex,
oct,
str and
val might be considered conversion functions, too.