开发者

Detect if NSString is a float number

开发者 https://www.devze.com 2023-03-29 18:01 出处:网络
I am tryingto dectect if an NSString it\'s a float开发者_如何学编程 number, for exemple : @\"-73.041382\"

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

0

精彩评论

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