Convert to absolute value in Objective-C

ID : 20372

viewed : 7

Tags : objective-cobjective-c

Top 5 Answer for Convert to absolute value in Objective-C

vote vote

99

Depending on the type of your variable, one of abs(int), labs(long), llabs(long long), imaxabs(intmax_t), fabsf(float), fabs(double), or fabsl(long double).

Those functions are all part of the C standard library, and so are present both in Objective-C and plain C (and are generally available in C++ programs too.)

(Alas, there is no habs(short) function. Or scabs(signed char) for that matter...)


Apple's and GNU's Objective-C headers also include an ABS() macro which is type-agnostic. I don't recommend using ABS() however as it is not guaranteed to be side-effect-safe. For instance, ABS(a++) will have an undefined result.


If you're using C++ or Objective-C++, you can bring in the <cmath> header and use std::abs(), which is templated for all the standard integer and floating-point types.

vote vote

86

You can use this function to get the absolute value:

+(NSNumber *)absoluteValue:(NSNumber *)input {   return [NSNumber numberWithDouble:fabs([input doubleValue])]; } 
vote vote

74

vote vote

69

By using cursor you can easily iterate through records individually and print records separately or as a single message including all the records.

DECLARE @CustomerID as INT; declare @msg varchar(max) DECLARE @BusinessCursor as CURSOR;  SET @BusinessCursor = CURSOR FOR SELECT CustomerID FROM Customer WHERE CustomerID IN ('3908745','3911122','3911128','3911421')  OPEN @BusinessCursor;     FETCH NEXT FROM @BusinessCursor INTO @CustomerID;     WHILE @@FETCH_STATUS = 0         BEGIN             SET @msg = '{               "CustomerID": "'+CONVERT(varchar(10), @CustomerID)+'",               "Customer": {                 "LastName": "LastName-'+CONVERT(varchar(10), @CustomerID) +'",                 "FirstName": "FirstName-'+CONVERT(varchar(10), @CustomerID)+'",                   }             }|'         print @msg     FETCH NEXT FROM @BusinessCursor INTO @CustomerID; END 
vote vote

58

Just another approach if you are fine using temp tables.I have personally tested this and it will not cause any exception (even if temp table does not have any data.)

CREATE TABLE #TempTable (     ROWID int identity(1,1) primary key,     HIERARCHY_ID_TO_UPDATE int, )  --create some testing data --INSERT INTO #TempTable VALUES(1) --INSERT INTO #TempTable VALUES(2) --INSERT INTO #TempTable VALUES(4) --INSERT INTO #TempTable VALUES(6) --INSERT INTO #TempTable VALUES(8)  DECLARE @MAXID INT, @Counter INT  SET @COUNTER = 1 SELECT @MAXID = COUNT(*) FROM #TempTable  WHILE (@COUNTER <= @MAXID) BEGIN     --DO THE PROCESSING HERE      SELECT @HIERARCHY_ID_TO_UPDATE = PT.HIERARCHY_ID_TO_UPDATE     FROM #TempTable AS PT     WHERE ROWID = @COUNTER      SET @COUNTER = @COUNTER + 1 END   IF (OBJECT_ID('tempdb..#TempTable') IS NOT NULL) BEGIN     DROP TABLE #TempTable END 

Top 3 video Explaining Convert to absolute value in Objective-C

Related QUESTION?