Name

Abs Function

Syntax

                  result = Abs(number)
number

Use: Required

Data Subtype: Any valid numeric expression

The number whose absolute value is to be returned.

Return Value

The absolute value of number . The datatype is the same as that passed to the function.

Description

Returns the absolute value of a number (i.e., its unsigned magnitude). For example, Abs(-1) and Abs(1) both return 1. If number contains Null, Null is returned; if it’s an uninitialized variable, zero is returned.

Rules at a Glance

Only numeric values can be passed to the Abs function.

Example

In this example, a variant of subtype Boolean is used within the script. However, a database field used to store the flag expects either 1 for True or for False. Therefore, because the VBScript Boolean True value is -1, before writing to the database, you must first convert the True value to its unsigned magnitude of 1:

<%
'bFlag will have a value of either True or False
'convert the boolean to an integer -1 or 0
   iFlag = cInt(bFlag)
'use Abs function to obtain the absolute value
   iFlag = Abs(iFlag)
'iFlag will now contain either 1 or 0
%>

Programming Tips & Gotchas

Because the Abs function accepts only numeric values, you may want to check the value you pass to Abs using the IsNumeric function to prevent generating an error. This is illustrated in the following code snippet:

<%
If IsNumeric(vExtent) Then
   Extent = Abs(vExtent)
   ...
End If
<%

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.