C# .Sign()

ksuraev's avatar
Published Nov 16, 2025
Contribute to Docs

The Math.Sign() method is a static method that returns an integer value indicating the sign of a specified number.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

Syntax

Math.Sign(number);

Parameters:

  • number: The numeric value for which to determine the sign. Supported types include: sbyte, short, int, long, float, double, and decimal.

Return value:

Returns an integer value that indicates the sign of the specified number:

  • -1: if the number is negative
  • 0: if the number is equal to zero
  • 1: if the number is positive

Example: Basic Usage of Math.Sign()

In this example, the sign of various numeric values is determined using the Math.Sign() method and printed to the console:

using System;
public class Example {
public static void Main () {
// Example numeric values
int numInt = -50;
float numFloat = 0.0f;
double numDouble = 2.7;
decimal numDecimal = -0.01m;
// Determine the sign of each number
int numIntSign = Math.Sign(numInt);
int numFloatSign = Math.Sign(numFloat);
int numDoubleSign = Math.Sign(numDouble);
int numDecimalSign = Math.Sign(numDecimal);
// Print the results
Console.WriteLine($"Math.Sign({numInt}) = {numIntSign}");
Console.WriteLine($"Math.Sign({numFloat}) = {numFloatSign}");
Console.WriteLine($"Math.Sign({numDouble}) = {numDoubleSign}");
Console.WriteLine($"Math.Sign({numDecimal}) = {numDecimalSign}");
}
}

This example outputs the following:

Math.Sign(-50) = -1
Math.Sign(0) = 0
Math.Sign(2.7) = 1
Math.Sign(-0.01) = -1

Codebyte Example

In this example, a random number between -100 and 100 is generated, and its sign is determined using the Math.Sign() method. A message is printed to indicate whether the random number is negative, positive, or zero:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours