How do I check if a number is positive or negative in C#?
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
num < 0 // number is negative
You youngins and your fancy less-than signs.
Back in my day we had to use Math.abs(num) != num //number is negative
!
public static bool IsNegative<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) < 0;
}
bool isNegative(int n) {
int i;
for (i = 0; i <= Int32.MaxValue; i++) {
if (n == i)
return false;
}
return true;
}