Hi all, I wrote some code to parse doubles from strings in a locale (pt_BR) where the grouping and decimal separators are "." and "," respectively (the exact opposite of en_US). It should be a piece of cake, but I'm not getting the correct results. Can someone please tell me what obvious fact I'm missing? I've been staring at this code for a while now and can't figure out what's wrong.
The code below is from an iOS test project created just for this scanner issue. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self.window makeKeyAndVisible]; double num; num = [self scanDouble: @"1,23"]; NSLog(@"result: %f", num); num = [self scanDouble: @"12,34"]; NSLog(@"result: %f", num); num = [self scanDouble: @"123,45"]; NSLog(@"result: %f", num); num = [self scanDouble: @"1234,56"]; NSLog(@"result: %f", num); num = [self scanDouble: @"1.234,56"]; NSLog(@"result: %f", num); num = [self scanDouble: @"12.345,67"]; NSLog(@"result: %f", num); return YES; } - (double) scanDouble: (NSString*) stringToScan; { NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier: @"pt_BR"]; static BOOL doneOnce = NO; if (! doneOnce) { doneOnce = YES; NSLog(@" group sep: %@", [locale objectForKey: NSLocaleGroupingSeparator]); NSLog(@"decimal sep: %@", [locale objectForKey: NSLocaleDecimalSeparator]); } NSLog(@"string to scan: '%@'", stringToScan); NSScanner* scanner = [[NSScanner alloc] initWithString: stringToScan]; [scanner setLocale: locale]; double num; if ([scanner scanDouble: &num]) { [scanner release]; return num; } else { NSLog(@"*** ERROR scanning '%@' for a double", stringToScan); [scanner release]; return 0.0; } } Here are the results: [Session started at 2010-12-11 00:57:52 -0200.] 2010-12-11 00:57:54.553 ScannerTest[82954:207] group sep: . 2010-12-11 00:57:54.554 ScannerTest[82954:207] decimal sep: , 2010-12-11 00:57:54.562 ScannerTest[82954:207] string to scan: '1,23' 2010-12-11 00:57:54.564 ScannerTest[82954:207] result: 1.230000 2010-12-11 00:57:54.566 ScannerTest[82954:207] string to scan: '12,34' 2010-12-11 00:57:54.566 ScannerTest[82954:207] result: 12.340000 2010-12-11 00:57:54.567 ScannerTest[82954:207] string to scan: '123,45' 2010-12-11 00:57:54.568 ScannerTest[82954:207] result: 123.450000 2010-12-11 00:57:54.568 ScannerTest[82954:207] string to scan: '1234,56' 2010-12-11 00:57:54.569 ScannerTest[82954:207] result: 1234.560000 2010-12-11 00:57:54.569 ScannerTest[82954:207] string to scan: '1.234,56' 2010-12-11 00:57:54.569 ScannerTest[82954:207] result: 1.000000 2010-12-11 00:57:54.570 ScannerTest[82954:207] string to scan: '12.345,67' 2010-12-11 00:57:54.576 ScannerTest[82954:207] result: 12.000000 Terminating in response to SpringBoard's termination. Note that the scanner returns the wrong result when the string to be parsed contains a '.' but I can't see why. Thanks in advance. WT _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com