In C#, Math.Sign(value) tells you the sign of a number. It always returns an int: -1 if the number is below zero, 0 if it is zero, and 1 if it is above zero.
Think of a traffic light with three states: down, stop, up. The size of the number does not matter. Math.Sign(-1000) and Math.Sign(-1) are both -1. This is handy when you only care about direction, such as "is the balance falling, steady, or rising?".
This is a static member of the static class System.Math. You call it with the class name, such as Math.Sign(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Sign is available in every .NET version, with overloads for int, long, double, decimal, and others.
Syntax
Pass one number. The answer is always an int, whatever type you passed in.
C#
int a = Math.Sign(-25); // -1
int b = Math.Sign(0); // 0
int c = Math.Sign(42.7); // 1
Call
What it does
Math.Sign(-25)
Returns -1.
Math.Sign(0)
Returns 0.
Math.Sign(-0.5)
Returns -1. Works for double and decimal as well.
Math.Sign(double.NaN)
Throws ArithmeticException. NaN has no sign.
The return type is always int, even for a double or decimal input, so you can use it directly in a switch or in arithmetic. For a double, both 0.0 and -0.0 give 0. Only NaN is a problem: check with double.IsNaN first.
Library declarations
C#
public static int Sign(int value);
public static int Sign(long value);
public static int Sign(float value);
public static int Sign(double value);
public static int Sign(decimal value);
// also short, sbyte, and nint overloads
Parameters
Parameter
Description
value
Required. The number to test. It can be an int, long, double, decimal, and so on. For floating-point types it must not be NaN.
Return Value
Return Value
Description
int
-1 if value is negative, 0 if it is zero, 1 if it is positive.
C# Examples
Example 1 — Negative, Zero, and Positive
Call Sign on four numbers and look at the three possible answers.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Sign(-25));
Console.WriteLine(Math.Sign(0));
Console.WriteLine(Math.Sign(42));
Console.WriteLine(Math.Sign(-0.5));
}
}
Output
-1
0
1
-1
How It Works
Below zero.-25 is negative, so the answer is -1. It would be -1 for -2 or -2000000 too.
Exactly zero. Zero is neither negative nor positive, so the answer is 0.
Above zero.42 is positive, so you get 1. The size of the number never changes the answer.
Doubles work the same way.-0.5 is a double. The double overload also returns an int, here -1.
Example 2 — Rising, Steady, or Falling
Use the sign of a difference to pick a word, so you do not need three separate if blocks.
C#
using System;
class Program {
static string Describe(int change) {
switch (Math.Sign(change)) {
case -1: return "falling";
case 0: return "steady";
default: return "rising";
}
}
static void Main() {
Console.WriteLine(Describe(20 - 35));
Console.WriteLine(Describe(50 - 50));
Console.WriteLine(Describe(90 - 12));
}
}
Output
falling
steady
rising
How It Works
A tiny helper.Describe takes the change between two readings and turns it into a word.
Switch on the sign.Math.Sign(change) can only be -1, 0, or 1, so a switch with three cases covers everything.
-15 is falling.20 - 35 is -15. Its sign is -1, so the first case returns falling.
Size does not matter.50 - 50 is 0 and gives steady. 90 - 12 is 78, positive, so the default case returns rising.
Example 3 — NaN Throws, Infinity and decimal Work
See why you should guard against NaN, and how infinity and decimal behave.
C#
using System;
class Program {
static void Main() {
double value = double.NaN;
if (double.IsNaN(value)) {
Console.WriteLine("skip NaN");
}
try {
Console.WriteLine(Math.Sign(value));
} catch (ArithmeticException) {
Console.WriteLine("NaN has no sign");
}
Console.WriteLine(Math.Sign(double.PositiveInfinity));
Console.WriteLine(Math.Sign(-7.5m));
}
}
Output
skip NaN
NaN has no sign
1
-1
How It Works
Guard first.double.IsNaN(value) is True, so the program prints skip NaN. In real code you would stop here.
Ignoring the guard. Calling Math.Sign on NaN throws ArithmeticException. The catch block catches it and prints a message instead of crashing.
Infinity has a sign. Positive infinity is above zero, so Math.Sign returns 1.
decimal overload. The m suffix makes -7.5m a decimal. Sign has a decimal version, and it returns -1.
Frequently Asked Questions
It returns an int: -1 if the number is negative, 0 if it is zero, and 1 if it is positive. The size of the number does not change the answer.
NaN means "not a number", so it is neither negative, zero, nor positive. Math.Sign throws ArithmeticException instead of guessing. Check double.IsNaN(value) first if your value might be NaN.
It returns 0. Negative zero and positive zero both count as zero. To see the sign bit of a double, use double.IsNegative(value).
No. Math is a static class, so you call Math.Sign(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.
Abs removes the sign and keeps the size, so Abs(-5) is 5. Sign throws away the size and keeps only the direction, so Sign(-5) is -1. Together they rebuild a number: Sign(x) * Abs(x) equals x.