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:
- Math.Sign(Decimal): Returns the integer that specifies the sign of a decimal number.
- Math.Sign(Double): Returns the integer that specifies the sign of a double-precision floating-point number.
- Math.Sign(Int16): Returns the integer that specifies the sign of a 16-bit signed integer. Here Int16 is short data type
- Math.Sign(Int32): Returns the integer that specifies the sign of a 32-bit signed integer. Here Int32 is int data type.
- Math.Sign(Int64): Returns the integer that specifies the sign of a 64-bit signed integer. Here Int64 is long data type.
- Math.Sign(SByte): Returns the integer that specifies the sign of an 8-bit signed integer.
- Math.Sign(Single): Returns the integer that specifies the sign of a single-precision floating-point number. Here single is float data type.
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:
using System;
class GFG {
static void Main(string[] args)
{
Decimal de = 150M;
Double d = 34.5432d;
short sh = 0;
int i = -5678;
long l = 598964564;
sbyte sb = -34;
float f = 56.89f;
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)));
}
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