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

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

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

相关标签:
17条回答
  • 2020-12-04 06:14

    First parameter is stored in EAX register and result also.

    function IsNegative(ANum: Integer): LongBool; assembler;
    asm
       and eax, $80000000
    end;
    
    0 讨论(0)
  • 2020-12-04 06:18

    You just have to compare if the value & its absolute value are equal:

    if (value == Math.abs(value))
        return "Positif"
    else return "Negatif"
    
    0 讨论(0)
  • 2020-12-04 06:20
    bool positive = number > 0;
    bool negative = number < 0;
    
    0 讨论(0)
  • 2020-12-04 06:21

    This is the industry standard:

    int is_negative(float num)
    {
      char *p = (char*) malloc(20);
      sprintf(p, "%f", num);
      return p[0] == '-';
    }
    
    0 讨论(0)
  • 2020-12-04 06:23

    Of course no-one's actually given the correct answer,

    num != 0   // num is positive *or* negative!
    
    0 讨论(0)
  • 2020-12-04 06:26

    The Math.Sign method is one way to go. It will return -1 for negative numbers, 1 for positive numbers, and 0 for values equal to zero (i.e. zero has no sign). Double and single precision variables will cause an exception (ArithmeticException) to be thrown if they equal NaN.

    0 讨论(0)
提交回复
热议问题