Name
CLng Function
Syntax
CLng(expression)-
expression Use: Required
Data Subtype: Numeric or String
The range of
expressionis -2,147,483,648 to 2,147,483,647; fractions are rounded.
Return Value
expression cast as a Variant with a
subtype of Long.
Description
Converts expression to a Variant with a
subtype of Long; 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 datatype, an overflow error is generated.When the fractional part is exactly 0.5,
CLngalways rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.
Example
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
Option Explicit
Sub cmdAdd_OnClick( )
Dim lSum, sNum1, sNum2
sNum1 = Window.Document.frmAdd.txtText1.Value
sNum2 = Window.Document.frmAdd.txtText2.Value
If IsNumeric(sNum1) And IsNumeric(sNum2) Then
lSum = CLng(sNum1) + CLng(sNum2)
Alert "The sum is: " & lSum
Else
Alert "The values you enter in the text boxes must be numeric."
End If
End Sub
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="frmAdd">
<INPUT TYPE="text" NAME="txtText1"><BR>
<INPUT TYPE="text" NAME="txtText2"><BR>
<INPUT TYPE="button" NAME="cmdAdd" VALUE="Sum">
</FORM>
</BODY>
</HTML>Programming Tips & Gotchas
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