Name
Sgn Function
Syntax
Sgn(number
)
-
number
Use: Required
Data Subtype: Any expression capable of conversion into a numeric value
A numeric expression.
Return Value
A Variant of subtype 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... |
|
---|---|
Positive |
1 |
Zero |
0 |
Negative |
-1 |
Programming Tips & Gotchas
If you’re planning on using the
Sgn
function to evaluate a result toFalse
(0) orTrue
(any nonzero value), you could also use theCBool
function.Sgn
—and a fairly trivial one—is to determine the sign of an expression. It’s equivalent to the following code:Public Function Sgn(varNumber as Variant) as Integer If varNumber > 0 Then Sgn = 1 ElseIf varNumber = 0 Then Sgn = 0 Else Sgn = -1 End If
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 theIsNumeric
function before the call toSgn
.
See Also
If . . . Then . . . Else Statement |
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.