smily
smily

Reputation: 347

getting 0.0000 when converting NSString value to float

In my application, i tried to convert NSString value to float using

NSString *value = [dict objectForKey:@"student_Percentage"];
float f = [value floatValue];

But I'm getting the value of f as 0.00000. I tried with NSNumberFormatter, NSNumber... but still get 0.00000.

Upvotes: -1

Views: 819

Answers (2)

Nims
Nims

Reputation: 431

Hey i think your dict value has a problem. Try this code it gives the correct value.

NSString *temp = @"25.38";
float p = [temp floatValue];
NSLog(@"%f",p);

Or maybe use valueforkey instead of objectForKey.

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

floatValue returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.

[dict objectForKey:@"student_Percentage"] value should be like 8.9.
Remove double quotes from your string.

NSMutableString *value = [dict objectForKey:@"student_Percentage"];
[value replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [value length])];
float f = [value floatValue];

Upvotes: 2

Related Questions