Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Sign() Method in C#
The Math.Sign() method in C# is used to return an integer that indicates the sign of a number. It returns +1 for positive numbers, -1 for negative numbers, and 0 for zero.
Syntax
The Math.Sign() method has multiple overloads to handle different numeric types −
public static int Sign(sbyte value); public static int Sign(short value); 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);
Return Value
The method returns an int value −
-1 if the value is negative
0 if the value is zero
+1 if the value is positive
Using Math.Sign() with Integer Types
using System;
public class Demo {
public static void Main() {
short val1 = 1;
int val2 = 20;
long val3 = -3545465777;
int val4 = 0;
Console.WriteLine("Short Value = " + val1);
Console.WriteLine("Sign (Short Value) = " + Math.Sign(val1));
Console.WriteLine("Int32 value = " + val2);
Console.WriteLine("Sign (Int32 Value) = " + Math.Sign(val2));
Console.WriteLine("Long value = " + val3);
Console.WriteLine("Sign (Long Value) = " + Math.Sign(val3));
Console.WriteLine("Zero value = " + val4);
Console.WriteLine("Sign (Zero Value) = " + Math.Sign(val4));
}
}
The output of the above code is −
Short Value = 1 Sign (Short Value) = 1 Int32 value = 20 Sign (Int32 Value) = 1 Long value = -3545465777 Sign (Long Value) = -1 Zero value = 0 Sign (Zero Value) = 0
Using Math.Sign() with Floating-Point Types
using System;
public class Demo {
public static void Main() {
decimal val1 = 20.5m;
double val2 = -35.252d;
float val3 = 0.0f;
Console.WriteLine("Decimal Value = " + val1);
Console.WriteLine("Sign (Decimal Value) = " + Math.Sign(val1));
Console.WriteLine("Double value = " + val2);
Console.WriteLine("Sign (Double Value) = " + Math.Sign(val2));
Console.WriteLine("Float value = " + val3);
Console.WriteLine("Sign (Float Value) = " + Math.Sign(val3));
}
}
The output of the above code is −
Decimal Value = 20.5 Sign (Decimal Value) = 1 Double value = -35.252 Sign (Double Value) = -1 Float value = 0 Sign (Float Value) = 0
Practical Example with Sign Interpretation
using System;
public class Demo {
public static void Main() {
double[] numbers = { -42.5, 0, 15.8, -100 };
foreach (double num in numbers) {
int sign = Math.Sign(num);
string interpretation = GetSignInterpretation(sign);
Console.WriteLine($"Number: {num}, Sign: {sign}, Interpretation: {interpretation}");
}
}
public static string GetSignInterpretation(int sign) {
if (sign == 0)
return "Zero";
else if (sign < 0)
return "Negative";
else
return "Positive";
}
}
The output of the above code is −
Number: -42.5, Sign: -1, Interpretation: Negative Number: 0, Sign: 0, Interpretation: Zero Number: 15.8, Sign: 1, Interpretation: Positive Number: -100, Sign: -1, Interpretation: Negative
Common Use Cases
Conditional logic based on number positivity or negativity
Mathematical calculations requiring sign determination
Data validation to check if values are positive, negative, or zero
Sorting algorithms that need to consider sign differences
Conclusion
The Math.Sign() method in C# provides a simple way to determine the sign of any numeric value, returning -1 for negative, 0 for zero, and +1 for positive numbers. It works with all numeric data types and is commonly used in conditional logic and mathematical operations.
