How do I get an absolute value from NSNumber

Discussion in 'iOS Programming' started by John Baughman, Feb 20, 2010.

  1. John Baughman macrumors member

    Joined:
    Oct 27, 2003
    #1
    I am having trouble making this work...

    Code:
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ihrs West",[[statusInfo objectAtIndex:indexPath.row] unsignedIntValue]];
    
    The object in the statusInfo array is an NSNumber. If it contains the value -2, I want the cell.detailText.text to display 2hrs West. Instead I am getting -2hrs West. I can't figure out how to get the absolute value of the integer stored in the NSNumber. I have tried a lot of different things, but the above code seems to me to be the way to do it. What am I doing wrong.

    John
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #3
    You could just do a dirty hack and multiply the value by -1 if the value is negative

    OR

    You could use the c abs function on the value from the from the row,
    Code:
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ihrs West",abs([[statusInfo objectAtIndex:indexPath.row] intValue])];
    
    I think this should work.
     
  4. John Baughman thread starter macrumors member

    Joined:
    Oct 27, 2003
    #4
    thanks!! The abs function worked great.

    I jumped into Objective-C never having done any c programming. I really need to take a time out and review what is available in c and how to use it in Cocoa.

    Thanks again,

    John
     

Share This Page