C# Math - Sign() Method
The C# Sign() method returns an integer that indicates the sign of the argument. It returns zero if the argument is zero, 1 if the argument is greater than zero, -1 if the argument is less than zero. The method can be overloaded and it can take float, sbyte, int, long, double, decimal and short arguments.
Syntax
public static int Sign (float value); public static int Sign (sbyte value); public static int Sign (int value); public static int Sign (long value); public static int Sign (double value); public static int Sign (decimal value); public static int Sign (short value);
Parameters
value |
Specify a signed value. |
Return Value
Returns an integer that indicates the sign of the argument.
Exception
Throws ArithmeticException, if the argument is NaN.
Example:
In the example below, Sign() method returns an integer that indicates the sign of the argument.
using System; class MyProgram { static void Main(string[] args) { Console.WriteLine("Math.Sign(48.45) = " + Math.Sign(48.45)); Console.WriteLine("Math.Sign(-48.667) = " + Math.Sign(-48.667)); Console.WriteLine("Math.Sign(0) = " + Math.Sign(0)); Console.WriteLine("Math.Sign(Double.NegativeInfinity) = " + Math.Sign(Double.NegativeInfinity)); Console.WriteLine("Math.Sign(Double.PositiveInfinity) = " + Math.Sign(Double.PositiveInfinity)); } }
The output of the above code will be:
Math.Sign(48.45) = 1 Math.Sign(-48.667) = -1 Math.Sign(0) = 0 Math.Sign(Double.NegativeInfinity) = -1 Math.Sign(Double.PositiveInfinity) = 1
❮ C# Math Methods