objective-c 在objective-c/cocoa-touch中是否有一个方便的功能来找到最低的数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/844990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a convenient function in objective-c / cocoa-touch to find a lowest number?
提问by Thanks
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.
我有两个数字,需要返回较低的一个。有什么我可以使用的功能吗?当然这是一个简单的任务,我可以做一个 if 语句。我只是想知道。
回答by Alex Rozanski
If you're using ints, use the MIN()macro:
如果您使用的是整数,请使用MIN()宏:
MIN(25, 50); //Returns 25
If you're comparing two NSNumbers, then use the compare:method:
如果要比较两个NSNumbers,请使用以下compare:方法:
NSNumber *number, *secondNumber; //Assume 'number'=25, 'secondNumber'=50
NSComparisonResult result = [number compare:secondNumber];
return (result==NSOrderedDescending)?secondNumber:number; //Returns the 'number' NSNumber
回答by hbw
The C standard library includes several min()functions that, given two numbers, will return the lower of the two:
C 标准库包括几个min()函数,如果给定两个数字,将返回两者中较小的一个:
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>.
要使用这些,只需#include <math.h>.
回答by Madan gupta
For minimum no. use
对于最低数量。用
MIN(number1,number2);
For maximum no. use
对于最大数量。用
MAX(number1,number2);

