CInt Function

   
CInt Function

Named Arguments

No

Syntax

 CInt(   expression   ) 
expression (required; Numeric or String)

The range of expression is -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

  • expression must evaluate to a numeric value; otherwise , a type-mismatch error is generated.

  • If the value of expression is outside the range of the Integer data type, an overflow error is generated.

  • When the fractional part of expression is exactly .5, CInt always 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 If 

Programming 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 of Val , because the data type conversion functions take into account the system's regional settings. In particular, CInt recognizes the thousands separator if it's present in expression , whereas Val does not. For example, if expression is 1,234, then CInt successfully converts it to the integer value 1234, while Val converts it to 1.

  • Use IsNumeric to test whether expression evaluates to a number before performing the conversion.

  • CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return the same type of value as was passed in.

  • CInt converts an expression to a signed 32-bit integer. To convert an expression to an unsigned 32-bit integer, create an instance of the UInt32 structure, and call its Parse method.

  • Like most of the conversion functions, CInt is not actually a function in the Microsoft.VisualBasic namespace. Instead, it is similar to a Visual C++ macro; the compiler translates the function call into inline code.

VB.NET/VB 6 Differences

The VB.NET CInt function actually corresponds to the VB 6 CLng function, since both return 32-bit integers.

See Also

CLng Function, CShort Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net