CLng Function

   
CLng Function

Named Arguments

No

Syntax

 CLng(   expression   ) 
expression (required; 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

  • 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 .5, CLng always 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 If 

Programming 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 of Val , because the data type conversion function takes into account the system's regional settings. In particular, CLng is able to recognize the thousands separator if it's included in expression , while Val cannot. For example, if a user enters a value of 1,098,234 into a textbox, 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 the same type of value as was passed in.

  • CLng converts an expression to a signed long integer. To convert an expression to an unsigned long integer, create an instance of the UInt64 structure and call its Parse method.

  • Like most of the conversion functions, CLng 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 CLng function returns a 64-bit integer, whereas the VB 6 CLng function returns a 32-bit integer.

See Also

CInt 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