16

Possible Duplicate:
iphone how to check that a string is numeric only

I have one NSString, then i want check the string is number or not.

I mean

NSString *val = @"5555" ;

if(val isNumber ){
  return true;
}else{
  retun false;
}

How can I do this in Objective C?

Community
  • 1
  • 1
jaleel
  • 315
  • 1
  • 5
  • 15
  • 1
    What counts as a number? Integers? Floating point? Scientific notation? Hexadecimal with leading "0x"? Binary with leading "0b"? – outis Jan 07 '10 at 13:01

2 Answers2

66

Use [NSNumberFormatter numberFromString: s]. It returns nil if the specified string is non-numeric. You can configure the NSNumberFormatter to define "numeric" for your particular scenario.


#import <Foundation/Foundation.h>

int
main(int argc, char* argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLocale *l_en = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
    NSLocale *l_de = [[NSLocale alloc] initWithLocaleIdentifier: @"de_DE"];
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    [f setLocale: l_en];

    NSLog(@"returned: %@", [f numberFromString: @"1.234"]);

    [f setAllowsFloats: NO];
    NSLog(@"returned: %@", [f numberFromString: @"1.234"]);

    [f setAllowsFloats: YES];
    NSLog(@"returned: %@", [f numberFromString: @"1,234"]);

    [f setLocale: l_de];
    NSLog(@"returned: %@", [f numberFromString: @"1,234"]);

    [l_en release];
    [l_de release];
    [f release];
    [pool release];
}
Dewayne Christensen
  • 2,084
  • 13
  • 15
  • 6
    I like this answer a lot better than the dupe. Using Foundation is almost certainly preferable to sinking to using a RegEx. – RickDT Mar 07 '12 at 15:19
  • 2
    numberFromString is an instance method, not a class method, just as a clarification. – Matjan Feb 06 '14 at 19:22
9

You could use rangeOfCharacterFromSet::

@interface NSString (isNumber)
-(BOOL)isInteger;
@end

@interface _IsNumber
+(void)initialize;
+(void)ensureInitialization;
@end

@implementation NSString (isNumber)
static NSCharacterSet* nonDigits;
-(BOOL)isInteger {
    /* bit of a hack to ensure nonDigits is initialized. Could also 
       make nonDigits a _IsNumber class variable, rather than an 
       NSString class variable.
     */
    [_IsNumber ensureInitialization];
    NSRange nond = [self rangeOfCharacterFromSet:nonDigits];
    if (NSNotFound == nond.location) {
        return YES;
    } else {
        return NO;
    }
}
@end

@implementation _IsNumber
+(void)initialize {
    NSLog(@"_IsNumber +initialize\n");
    nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
}
+(void)ensureInitialization {}
@end
outis
  • 75,655
  • 22
  • 151
  • 221
  • Isn't this just a little bit overkill? What's wrong with using an NSScanner or (less reliably) just calling `floatValue` or `intValue` on the string? However, your method will return `YES` even for numbers consisting of thousands of digits, which may be more useful. Also, your character set would be autoreleased (if it is not cached by the runtime). – dreamlax Jan 07 '10 at 21:01
  • 1
    @dreamlax: `NSScanner` and the `*Value` methods will all accept strings that begin with a number but have other characters after that. Depending on the OP's actual purpose, this behavior may be better or may be unacceptable. `NSNumberFormatter` is probably the best solution, as it works for floating point and takes localization into account. – outis Jan 07 '10 at 22:45
  • This is not locale aware. – Rudolf Adamkovič Jul 08 '14 at 12:12
  • This is also useful if you have an extremely long string of numbers like a 34 digit tracking number. – TPoschel Jan 13 '15 at 21:46