Open In App

C# | Math.Sign() Method

In C#, Sign() is a math class method which returns an integer that specify the sign of the number. This method can be overloaded by changing the data type of the passed arguments as follows:

Common Syntax for all above methods:



public static int Sign(data_type value)

Parameter: This method takes a single parameter in the form of either bytes, int, double, sbyte, etc.

Return type : This method returns the value of type System.Int32 as per following mentioned conditions:



Return Value Condition:
0 If value is equal to zero
1 If value is greater than zero
-1 If value is lesser than zero

Example:




// C# program to demonstrate the 
// Math.Sign() method
using System;
  
class GFG {
      
    // Main Method
    static void Main(string[] args)
    {
  
        // Decimal data type
        Decimal de = 150M;
          
        // Double data type
        Double d = 34.5432d;
          
        // Int16 data type
        short sh = 0;
          
        // Int32 data type
        int i = -5678;
          
        // Int64 data type
        long l = 598964564;
          
        // SByte data type
        sbyte sb = -34;
          
        // float data type
        float f = 56.89f;
          
          
        // displaying result
        Console.WriteLine("Decimal: " + de + " " + check(Math.Sign(de)));
        Console.WriteLine("Double: " + d + " " + check(Math.Sign(d)));
        Console.WriteLine("Int16: " + sh + " " + check(Math.Sign(sh)));
        Console.WriteLine("Int32: " + i + " " + check(Math.Sign(i)));
        Console.WriteLine("Int64: " + l + " " + check(Math.Sign(l)));
        Console.WriteLine("SByte: " + sb + " " + check(Math.Sign(sb)));
        Console.WriteLine("Single: " + f + " " + check(Math.Sign(f)));
  
    }
  
    // function to check whether the input 
    // number is greater than zero or not
    public static String check(int compare)
    {
        if (compare == 0)
            return "equal to zero";
              
        else if (compare < 0)
            return "less than zero";
              
        else
            return "greater than zero";
    }
}

Output:
Decimal: 150 greater than zero
Double: 34.5432 greater than zero
Int16: 0 equal to zero
Int32: -5678 less than zero
Int64: 598964564 greater than zero
SByte: -34 less than zero
Single: 56.89 greater than zero

Article Tags :
C#