How to convert NSString value @"3.45" into float. 3.45
float fCost = [[NSDecimalNumber decimalNu开发者_StackOverflow社区mberWithString:@"3.45"]floatValue] ;
NSString *val = @"3.45";
float fCost = [val floatValue];
float number = [string floatValue];
NSString's: - (float)floatValue;
For example:
NSString *str = @"3.45";
float f = [str floatValue];
Reading your comment to Marek's answer it looks like your string actually = @"$3.45"
To convert that to a float use:
NSNumberFormatter * formatter = [[[NSNumberFormatter alloc] init] autorelease];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = @"USD";
float value = [[formatter numberFromString: @"$3.45"] floatValue];
精彩评论