A function to return a sign

前端 未结 8 1292
春和景丽
春和景丽 2021-01-17 08:40

I know this will really turn out to be simple, but my brain is just not working. I need a function in C# that will return -1 if the integer passed to the function has a nega

相关标签:
8条回答
  • 2021-01-17 08:53
    public int SignFunction(int number) {
      return (number > 0) ? 
                 1 : (number < 0) ? 
                        -1 : number;
    }
    
    0 讨论(0)
  • 2021-01-17 08:58
    return input.CompareTo(0);
    
    0 讨论(0)
  • 2021-01-17 09:00
    int sign = Math.Sign(number);
    

    It already exists.

    0 讨论(0)
  • 2021-01-17 09:01
    public int Sign(int number)
    {
        if(number==0)
           return 0;
        return number/Math.Abs(number);
    }
    
    0 讨论(0)
  • 2021-01-17 09:05

    If Math.Sign did not exist, I would do this:

    return x == 0 ? 0 : x / Math.Abs(x);
    
    0 讨论(0)
  • 2021-01-17 09:14
    public int SignFunction( int input )
    {
        if( input < 0 ) return -1;
        if( input > 0 ) return 1;
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题