Sgn - VBScript


Overview

The Sgn command in VBScript determines the sign of a given numeric expression. It returns a value of 1 if the expression is positive, 0 if it is zero, and -1 if it is negative. This command is particularly useful for comparisons and mathematical calculations.

Syntax

Sgn(expression)

| Argument | Description |
|—|—|
| expression | Any numeric expression whose sign is to be determined. |

Options/Flags

None.

Examples

Simple Example:

Dim myNumber = 123
Dim signOfMyNumber = Sgn(myNumber)
WScript.Echo "Sign of " & myNumber & " is: " & signOfMyNumber

Complex Example:

' Check if a number is positive, negative, or zero using Sgn function
Dim number = -456
Select Case Sgn(number)
    Case 1
        WScript.Echo "The number is positive."
    Case -1
        WScript.Echo "The number is negative."
    Case 0
        WScript.Echo "The number is zero."
End Select

Common Issues

  • Syntax Errors: Ensure the expression provided to the Sgn function is a valid numeric expression.

Integration

The Sgn command can be integrated with other VBScript commands to perform conditional operations, such as:

If Sgn(x) = 1 Then
    ' Do something when x is positive
ElseIf Sgn(x) = -1 Then
    ' Do something when x is negative
Else
    ' Do something when x is zero
End If
  • Abs: Returns the absolute value of a number.
  • Round: Rounds a number to the specified number of digits.
  • Fix: Truncates a number to the integer portion.