Name
CInt Function
Named Arguments
No
Syntax
CInt(expression)-
expression Use: Required
Data Type: Numeric or String
The range of
expressionis -2,147,483,648 to 2,147,483,647; fractions are rounded.
Return Value
expression cast as an Integer
Description
Converts expression to an Integer; any
fractional portion 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 Integer data type, an overflow error is generated.When the fractional part of
expressionis exactly .5,CIntalways rounds it to the nearest even number. For example, .5 rounds to 0, and 1.5 rounds to 2.
Example
Dim iMyNumber as Integer
If IsNumeric(sMyNumber) then
iMyNumber = CInt(sMyNumber)
End IfProgramming Tips and Gotchas
When converting a string representation of a number to a numeric data type, you should use the data type conversion functions—such as
CInt—instead ofVal, because the data type conversion functions take into account the system’s regional settings. In particular,CIntrecognizes the thousands separator if it’s present inexpression, whereasValdoes not. For example, ifexpressionis 1,234, thenCIntsuccessfully converts it to the integer value 1234, whileValconverts it to 1.Use
IsNumericto test whetherexpressionevaluates to a number before performing the conversion.CIntdiffers from theFixandIntfunctions, which truncate, rather than round, the fractional ...
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