[ACCEPTED]-objective-C - other methods to convert a float to string-objective-c

Accepted answer
Score: 47

You could use NSNumber

NSString *myString = [[NSNumber numberWithFloat:myFloat] stringValue];

But there's no problem doing 2 it the way you are, in fact the way you 1 have in your question is better.

Score: 13

in a simple way

NSString *floatString = @(myFloat).stringValue;

0

Score: 6

You can create your own category method. Something 3 like

@interface NSString (Utilities)

+ (NSString *)stringWithFloat:(CGFloat)float

@end

@implementation NSString (Utilities)

+ (NSString *)stringWithFloat:(CGFloat)float
{
    NSString *string = [NSString stringWithFormat:@"%f", float];
    return string;
}

@end

Edit

Changed this to a class method and also 2 changed the type from float to CGFloat.

You can use 1 it as:

NSString *myFloat = [NSString stringWithFloat:2.1f];
Score: 4
float someVal = 22.3422f;
NSNumber* value = [NSNumber numberWithFloat:someVal];
NSLog(@"%@",[value stringValue]);

0

More Related questions