Search code examples
objective-cnsnumberformattercgfloat

Objective-C: Separate number right/left of decimal


I am trying to separate the left and right numbers from a float so I can turn each into a string. So for example, 5.11, I need to be able to turn 5 into a string, and 11 into another string.

// from float: 5.11
NSString * leftNumber  = @"5";
NSString * rightNumber = @"11";

From this, I can turn 5.11 into 5' 11".


Solution

  • One way:

    1. Use stringWithFormat to convert to string "5.11".
    2. Use componentsSeparatedByString to seperate into an array of "5" and "11".
    3. Combine with stringWithFormat.