0

I've tried looking around and can't seem to find anything to something that I thought should be fairly simple to implement. Basically I just want to find a way to take a list of numbers and shorten them down to an abbreviated format (for example 1000 to 1k and 500000 to 500k). I tried using this below, and it's working so far but I feel like I'm just going to have to cycle through every possible format so I'm wondering if there was something simpler to do this or if I just need to use this method.

-(NSString *)formatStringFromNumber:(int)num {

NSString *value = nil;
if (num >= 100000) {
    value = [NSString stringWithFormat:@"$%dk", (num/1000)];
}
if (num >= 10000) {
    value = [NSString stringWithFormat:@"$%dk", (num/1000)];
}
if (num >= 100000000) {
    value = [NSString stringWithFormat:@"$%dM", (num/1000000)];
}

return value;

}

Sal Aldana
  • 1,235
  • 4
  • 17
  • 39
  • Not really a duplicate since the post you provided was geared towards converting bytes into readable formats. I'll see if I can tweak it for what I need, but I'm trying to convert some numbers into shortened currency values for a graph's axis labels. thanks – Sal Aldana Dec 26 '13 at 20:31
  • Keep in mind that you can subclass NSByteCountFormatter or wrap it to meet your needs. – zaph Dec 26 '13 at 20:51
  • If you are looking for built-in Apple function then there isn't one that I am aware of. Sah's answer is correct look down and check "dbr" answer in that link. It's pretty close to what you got. – Sam B Dec 26 '13 at 21:26

0 Answers0