How do I check if a number is positive or negative in C#?

前端 未结 17 1407
轻奢々
轻奢々 2020-12-04 05:55

How do I check if a number is positive or negative in C#?

相关标签:
17条回答
  • 2020-12-04 06:28
        public static bool IsPositive<T>(T value)
            where T : struct, IComparable<T>
        {
            return value.CompareTo(default(T)) > 0;
        }
    
    0 讨论(0)
  • 2020-12-04 06:29
    num < 0 // number is negative
    
    0 讨论(0)
  • 2020-12-04 06:32

    You youngins and your fancy less-than signs.

    Back in my day we had to use Math.abs(num) != num //number is negative !

    0 讨论(0)
  • 2020-12-04 06:35
    public static bool IsNegative<T>(T value)
       where T : struct, IComparable<T>
    {
        return value.CompareTo(default(T)) < 0;
    }
    
    0 讨论(0)
  • 2020-12-04 06:36
    bool isNegative(int n) {
      int i;
      for (i = 0; i <= Int32.MaxValue; i++) {
        if (n == i) 
          return false;
      }
      return true;
    }
    
    0 讨论(0)
提交回复
热议问题