如何将NSString值@“3.45”转换为float。 3.45
float fCost = [[NSDecimalNumber decimalNumberWithString:@"3.45"]floatValue] ;
答案 0 :(得分:55)
NSString *val = @"3.45";
float fCost = [val floatValue];
答案 1 :(得分:9)
float number = [string floatValue];
答案 2 :(得分:4)
NSString:- (float)floatValue;
例如:
NSString *str = @"3.45";
float f = [str floatValue];
答案 3 :(得分:4)
阅读您对Marek的回答的评论看起来您的字符串实际上是= @"$3.45"
要将其转换为浮动使用:
NSNumberFormatter * formatter = [[[NSNumberFormatter alloc] init] autorelease];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = @"USD";
float value = [[formatter numberFromString: @"$3.45"] floatValue];