[ACCEPTED]-objective-C - other methods to convert a float to string-objective-c
Accepted answer
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.
in a simple way
NSString *floatString = @(myFloat).stringValue;
0
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];
float someVal = 22.3422f;
NSNumber* value = [NSNumber numberWithFloat:someVal];
NSLog(@"%@",[value stringValue]);
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.