开发者

How to convert NSString value @"3.45" into float?

开发者 https://www.devze.com 2023-04-10 06:40 出处:网络
How to convert NSString value @\"3.45\" into float. 3.45 float fCost = [[NSDecimalNumber decimalNu开发者_StackOverflow社区mberWithString:@\"3.45\"]floatValue] ;

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消