Is there a convenient function in objective-c / cocoa-touch to find a lowest number?

后端 未结 3 922
长情又很酷
长情又很酷 2020-12-24 05:10

I have two numbers, and need to get returned the lower one. Is there any function I could use? Sure it\'s a easy task, I could do an if-statement. I just want to know.

相关标签:
3条回答
  • 2020-12-24 05:43

    If you're using ints, use the MIN() macro:

    MIN(25, 50); //Returns 25
    

    If you're comparing two NSNumbers, then use the compare: method:

    NSNumber *number, *secondNumber; //Assume 'number'=25, 'secondNumber'=50
    NSComparisonResult result = [number compare:secondNumber];
    
    return (result==NSOrderedDescending)?secondNumber:number; //Returns the 'number' NSNumber
    
    0 讨论(0)
  • 2020-12-24 06:04

    The C standard library includes several min() functions that, given two numbers, will return the lower of the two:

     double fmin(double x, double y);
     long double fminl(long double x, long double y);
     float fminf(float x, float y);
    

    To use these, just #include <math.h>.

    0 讨论(0)
  • 2020-12-24 06:06

    For minimum no. use

    MIN(number1,number2);
    

    For maximum no. use

    MAX(number1,number2);
    
    0 讨论(0)
提交回复
热议问题