Name
CLng Function
Named Arguments
No
Syntax
CLng(expression)-
expression Use: Required
Data Type: Numeric or String
Ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807; fractions are rounded.
Return Value
expression cast as a Long data type
Description
Converts expression to a long integer; any
fractional element of expression is
rounded.
Rules at a Glance
expressionmust evaluate to a numeric value; otherwise, a type-mismatch error is generated.If the value of
expressionis outside the range of the Long data type, an overflow error is generated.When the fractional part is exactly .5,
CLngalways rounds it to the nearest even number. For example, .5 rounds to 0, and 1.5 rounds to 2.
Example
Dim lngMyNumber as Long
If IsNumeric(sMyNumber) then
lngMyNumber = CLng(sMyNumber)
End IfProgramming Tips and Gotchas
When converting a string representation of a number to a numeric, you should use the data type conversion functions—such as
CLng—instead ofVal, because the data type conversion function takes into account the system’s regional settings. In particular,CLngis able to recognize the thousands separator if it’s included inexpression, whileValcannot. For example, if a user enters a value of 1,098,234 into a textbox,CLngconverts it to the long integer 1098234, butValconverts it to a value of 1.Use
IsNumericto test whetherexpressionevaluates to a number.CLngdiffers from theFixandIntfunctions, which truncate, rather than round, the fractional part of a number. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access