I am trying to dectect if an NSString it's a float number, for exemple : @"-73.041382"
But when I try with this method I obtain a wrong result:
-(bool) isNumeric:(NSString*) checkText{
NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
NSNumber* number = [numberFormatter numberFromString:checkText];
if (number != nil) {
return true;
}
return false;
}
Someone have an idea!?
Thanks
I have found the answer :
-(bool) isNumeric:(NSString*) checkText{
NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
//Set the locale to US
[numberFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
//Set the number style to Scientific
[numberFormatter setNumberStyle:NSNumberFormatterScientificStyle];
NSNumber* number = [numberFormatter numberFromString:checkText];
if (number != nil) {
return true;
}
return false;
}
Thanks!
-(bool) isNumeric:(NSString*) checkText{
return [[NSScanner scannerWithString:checkText] scanFloat:NULL];
}
i'm currently not on mac so i can't check it, hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With