This is my sample code to convert a integer into string.
Is it very costly to do the multiple loops of [NSMutableString stringWithFormat:..]?
I.e should I just use appendString and get the NSString in reverse order and then do one for loop to reverse this "reverse string".
Is there any other faster/more elegant way?
NSString * intToString(NSInteger numberToConvert)
{
BOOL isNegative = (numberToConvert < 0);
if (isNegative)
{
numberToConvert = -1*numberToConvert;
}
NSMutableString *convertedString = [[NSMutableString alloc] init];
while(numberToConvert != 0)
{
convertedString = [NSMutableString stringWithFormat:@"%ld%@",numberToConvert%10,convertedString];
numberToConvert = numberToConvert/10;
}
if (isNegative)
{
convertedString = [NSMutableString stringWithFormat:@"-%@",convertedString];
}
return convertedString;
}