判断NSString能否转换为数字

判断是否能够转成整形

- (BOOL)isPureInt:(NSString *)string{
if (!string) {
    return NO;
}
NSScanner *_scanner = [NSScanner scannerWithString:string];
int val;
return [_scanner scanInt:&val] && [_scanner isAtEnd];
}

判断是否能转成浮点型

- (BOOL)isPureFloat:(NSString *)string{
if (!string) {
    return NO;
}
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return [scan scanFloat:&val] && [scan isAtEnd];
}
This entry was posted in iOS.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">