## Int Function

Named Arguments

Syntax

CInt( expression) expression

Use: Required

Data Type: Numeric or String

The range of expression is -32,768 to 32,767; fractions are rounded.

160 Chapter 7- The Language Reference

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 0.5, CInt always rounds it to the nearest even number. For example, 0.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 & Gotchas

• When converting a string representation of a number to a numeric, you should use the data type conversion functions—such as CInt-—instead of Val, because the data type conversion functions take account of the system's regional settings. In particular, CInt recognizes the thousands separator if it's present in expression, whereas Val doesn't. For example, if expression is 1,234, 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.

 See Also £ Fix Function, FormatNumber Function, Int Function, IsNumeric Function if CLng Function et» Named Arguments No CLng( expression) expression Use: Required CLng Function 161 VB & VBA in a Nutshell: The Language, eMatter Edition Copyright © 2000 O'Reilly & Associates, Inc. All rights reserved.

Data Type: 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 a Long data type.

Description

Converts expression to an long integer; any fractional element 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 long data type, an overflow error is generated.

• When the fractional part is exactly 0.5, CLng always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.

Example

Dim lngMyNumber as Long If IsNumeric(sMyNumber) then lngMyNumber = CLng(sMyNumber) End If

Programming Tips & Gotchas

• When converting a string representation of a number to a numeric, you should use the data type conversion functions—such as CLng—instead of Val, because the data type conversion function takes account of the system's regional settings. In particular, CLng recognizes the thousands separator if it's included in expression, while Val can't. For example, if a user enters a value of 1,098,234 into a text box, CLng converts it to the long integer 1098234, but Val converts it to a value of 1.

• Use IsNumeric to test whether expression evaluates to a number.

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

Fix Function, FormatNumber Function, Int Function, IsNumeric Function, Val Function