Export (0) Print
Expand All

Math.Sign Method

Returns a value indicating the sign of a number.

Overload List

Returns a value indicating the sign of a decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Decimal) As Integer
[C#] public static int Sign(decimal);
[C++] public: static int Sign(Decimal);
[JScript] public static function Sign(Decimal) : int;

Returns a value indicating the sign of a double-precision floating-point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Double) As Integer
[C#] public static int Sign(double);
[C++] public: static int Sign(double);
[JScript] public static function Sign(double) : int;

Returns a value indicating the sign of a 16-bit signed integer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Short) As Integer
[C#] public static int Sign(short);
[C++] public: static int Sign(short);
[JScript] public static function Sign(Int16) : int;

Returns a value indicating the sign of a 32-bit signed integer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Integer) As Integer
[C#] public static int Sign(int);
[C++] public: static int Sign(int);
[JScript] public static function Sign(int) : int;

Returns a value indicating the sign of a 64-bit signed integer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Long) As Integer
[C#] public static int Sign(long);
[C++] public: static int Sign(__int64);
[JScript] public static function Sign(long) : int;

Returns a value indicating the sign of an 8-bit signed integer. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(SByte) As Integer
[C#] public static int Sign(sbyte);
[C++] public: static int Sign(char);
[JScript] public static function Sign(SByte) : int;

Returns a value indicating the sign of a single-precision floating-point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Sign(Single) As Integer
[C#] public static int Sign(float);
[C++] public: static int Sign(float);
[JScript] public static function Sign(float) : int;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Sign. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' This example demonstrates Math.Sign()
Imports System

Class Sample
   Public Shared Sub Main()
      Dim str As String = "{0}: {1,3} is {2} zero."
      Dim nl As String = Environment.NewLine
      
      Dim xByte1 As Byte = 0
      Dim xShort1 As Short = -2
      Dim xInt1 As Integer = -3
      Dim xLong1 As Long = -4
      Dim xSingle1 As Single = 0F
      Dim xDouble1 As Double = 6.0
      Dim xDecimal1 As [Decimal] = -7D
      ' The following type is not CLS-compliant: SByte
      
      Console.WriteLine("{0}Test the sign of the following types of values:", nl)
      Console.WriteLine(str, "Byte   ", xByte1, Test(Math.Sign(xByte1)))
      Console.WriteLine(str, "Int16  ", xShort1, Test(Math.Sign(xShort1)))
      Console.WriteLine(str, "Int32  ", xInt1, Test(Math.Sign(xInt1)))
      Console.WriteLine(str, "Int64  ", xLong1, Test(Math.Sign(xLong1)))
      Console.WriteLine(str, "Single ", xSingle1, Test(Math.Sign(xSingle1)))
      Console.WriteLine(str, "Double ", xDouble1, Test(Math.Sign(xDouble1)))
      Console.WriteLine(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1)))
      '
      Console.WriteLine("{0}The following type is not supported: SByte", nl)
   End Sub 'Main
   '
   Public Shared Function Test([compare] As Integer) As [String]
      If [compare] = 0 Then
         Return "equal to"
      ElseIf [compare] < 0 Then
         Return "less than"
      Else
         Return "greater than"
      End If
   End Function 'Test
End Class 'Sample 
'
'This example produces the following results:
'
'Test the sign of the following types of values:
'Byte   :   0 is equal to zero.
'Int16  :  -2 is less than zero.
'Int32  :  -3 is less than zero.
'Int64  :  -4 is less than zero.
'Single :   0 is equal to zero.
'Double :   6 is greater than zero.
'Decimal:  -7 is less than zero.
'
'The following type is not supported: SByte
'

[C#] 
// This example demonstrates Math.Sign()
using System;

class Sample 
{
    public static void Main() 
    {
    string str = "{0}: {1,3} is {2} zero.";
    string nl = Environment.NewLine;

    byte     xByte1    = 0;    
    short    xShort1   = -2;
    int      xInt1     = -3;
    long     xLong1    = -4;
    float    xSingle1  = 0.0f;
    double   xDouble1  = 6.0;
    Decimal  xDecimal1 = -7m;

// The following type is not CLS-compliant.
    sbyte    xSbyte1   = -101;

    Console.WriteLine("{0}Test the sign of the following types of values:", nl);
    Console.WriteLine(str, "Byte   ", xByte1, Test(Math.Sign(xByte1)));
    Console.WriteLine(str, "Int16  ", xShort1, Test(Math.Sign(xShort1)));
    Console.WriteLine(str, "Int32  ", xInt1, Test(Math.Sign(xInt1)));
    Console.WriteLine(str, "Int64  ", xLong1, Test(Math.Sign(xLong1)));
    Console.WriteLine(str, "Single ", xSingle1, Test(Math.Sign(xSingle1)));
    Console.WriteLine(str, "Double ", xDouble1, Test(Math.Sign(xDouble1)));
    Console.WriteLine(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1)));
//
    Console.WriteLine("{0}The following type is not CLS-compliant.", nl);
    Console.WriteLine(str, "SByte  ", xSbyte1, Test(Math.Sign(xSbyte1)));
    }
//
    public static String Test(int compare)
    {
    if (compare == 0) 
       return "equal to";
    else if (compare < 0)  
        return "less than";
    else 
        return "greater than";
    }
}
/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/

[C++] 
// This example demonstrates Math.Sign()
#using <mscorlib.dll>
using namespace System;

String* Test(int compare)
{
    if (compare == 0) 
       return S"equal to";
    else if (compare < 0)  
        return S"less than";
    else 
        return S"greater than";
}

int main() 
{
    String* str = S"{0}: {1,3} is {2} zero.";
    String* nl = Environment::NewLine;

    Byte     xByte1    = 0;    
    short    xShort1   = -2;
    int      xInt1     = -3;
    long     xLong1    = -4;
    float    xSingle1  = 0.0f;
    double   xDouble1  = 6.0;
    Decimal  xDecimal1 = -7;

// The following type is not CLS-compliant.
    SByte    xSbyte1   = -101;

    Console::WriteLine(S"{0}Test the sign of the following types of values:", nl);
    Console::WriteLine(str, S"Byte   ", __box(xByte1), Test(Math::Sign(xByte1)));
    Console::WriteLine(str, S"Int16  ", __box(xShort1), Test(Math::Sign(xShort1)));
    Console::WriteLine(str, S"Int32  ", __box(xInt1), Test(Math::Sign(xInt1)));
    Console::WriteLine(str, S"Int64  ", __box(xLong1), Test(Math::Sign(xLong1)));
    Console::WriteLine(str, S"Single ", __box(xSingle1), Test(Math::Sign(xSingle1)));
    Console::WriteLine(str, S"Double ", __box(xDouble1), Test(Math::Sign(xDouble1)));
    Console::WriteLine(str, S"Decimal", __box(xDecimal1), Test(Math::Sign(xDecimal1)));
//
    Console::WriteLine(S"{0}The following type is not CLS-compliant.", nl);
    Console::WriteLine(str, S"SByte  ", __box(xSbyte1), Test(Math::Sign(xSbyte1)));
}
/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Math Class | Math Members | System Namespace

Show:
© 2014 Microsoft