kullanın [NSNumberFormatter numberFromString: s]
. Belirtilen dize sayısal değilse, nil değerini döndürür. NSNumberFormatter'ı, özel senaryonuz için "sayısal" olarak tanımlayacak şekilde yapılandırabilirsiniz.
#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];
}
Numara olarak ne sayılır? Tamsayılar? Kayan nokta? Bilimsel gösterim? Öncelikli "0x" ile onaltılık? Öncü "0b" ile ikili? – outis