我如何检查一个数字是正的或负的c# ?


当前回答

当然没有人给出正确答案,

num != 0   // num is positive *or* negative!

其他回答

num < 0 // number is negative
bool positive = number > 0;
bool negative = number < 0;
    public static bool IsPositive<T>(T value)
        where T : struct, IComparable<T>
    {
        return value.CompareTo(default(T)) > 0;
    }

你只需要比较是否值和它的绝对值相等:

if (value == Math.abs(value))
    return "Positif"
else return "Negatif"

这是行业标准:

int is_negative(float num)
{
  char *p = (char*) malloc(20);
  sprintf(p, "%f", num);
  return p[0] == '-';
}