29

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

float fCost = [[NSDecimalNumber decimalNumberWithString:@"3.45"]floatValue] ;
user891268
  • 1,425
  • 5
  • 20
  • 25

4 Answers4

57
NSString *val = @"3.45";
float fCost = [val floatValue];
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
9
float number = [string floatValue];
Jeffrey Neo
  • 3,693
  • 2
  • 26
  • 30
kiran
  • 4,285
  • 7
  • 53
  • 98
4

NSString's: - (float)floatValue;

For example:

NSString *str = @"3.45";
float f = [str floatValue];
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
4

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];
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160