tags:

views:

71

answers:

2

how do I convert string to float in objective-C?

I am trying to multiply a couple of strings I am getting back from JSON:

float subTotal = [[[[purchaseOrderItemsJSON objectAtIndex:i] objectAtIndex:j] objectForKey:@"Price"] floatValue];

NSLog(@"%@",subTotal);

This gives me: (null) in the console. I know that there is a valid string coming out of that array because I am already using it to display it's value in a label.

+2  A: 
your_float = [your_string floatValue];

EDIT:

try this:

  NSLog(@"float value is: %f", your_float);
Gollum
float subTotal = [[[[purchaseOrderItemsJSON objectAtIndex:i] objectAtIndex:j] objectForKey:@"Price"] floatValue]; NSLog(@"%@",subTotal); This gives me a (null) on console. I know there is a string value in there.
Slee
@Max, Can you try to print that string, and paste it here?
Gollum
it is: 3399 and the output ends up being: (null)
Slee
I am a dope - @"%@", I'll leave it at that
Slee
+2  A: 

Have you tried Google? Search for "objective-c string to float" results in first entry:

NSString Class Reference - floatValue

jigfox