O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Sgn Function

Syntax

Sgn(number)
number

Use: Required

Data Type: Any expression capable of conversion into a numeric value

A numeric expression.

Return Value

An Integer.

Description

Determines the sign of a number.

Rules at a Glance

The return value of the Sgn function is determined by the sign of number:

If number is...

Sgn returns

Positive

1

Zero

0

Negative

-1

Programming Tips and Gotchas

  • If you’re planning on using the Sgn function to evaluate a result to False (0) or True (any nonzero value), you could also use the CBool function.

  • The major use for Sgn—a fairly trivial one—is to determine the sign of an expression. It’s equivalent to the following code:

    Function Sgn(varNumber)
       If varNumber > 0 Then
          Sgn = 1
       ElseIf varNumber = 0 Then
          Sgn = 0
       Else
          Sgn = -1
       End If
    End Function
  • Sgn is useful in cases in which the sign of a quantity defines the sign of an expression. For example:

    lngResult = lngQty * Sgn(lngValue)
  • Although Sgn handles the conversion of strings to numeric data, it’s a good idea to make sure that number is valid by calling the IsNumeric function before the call to Sgn.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required