What is Math.Sign() in C#?
C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Sign() function, which is used to return the integer indicating the sign of a specified number.
Syntax
The function returns an integer that indicates the sign of param.
Sign (type param);
where:
value: is a signed number
Return value
-
0: Returns this if param = 0or
-
1: Returns this if param > 0or
-
-1: Returns this if param < 0
Variants
Sign(Single)
public static int Sign (float param);
Sign(Sbyte)
public static int Sign (sbyte param);
Sign(Int64)
public static int Sign (long param);
Sign(Decimal)
public static int Sign (decimal param);
Sign(Int16)
public static int Sign (short param);
Sign(Double)
public static int Sign (double param);
Sign(Int32)
public static int Sign (int param);
Examples
using System;class HelloWorld{static void Main(){System.Console.WriteLine(Math.Sign(2));System.Console.WriteLine(Math.Sign(-10.2563M));System.Console.WriteLine(Math.Sign(0));}}