NSNumberFormatter fails to convert NSString with a decimal number to NSNumber

Originator:srgtuszy
Number:rdar://11417364 Date Originated:09.05.2012
Status:Open Resolved:No
Product:Developer Tools Product Version:Xcode 4.3.2 (4E2002)
Classification:Other bugs Reproducible:Always
 
Summary:

NSNumberFormatter's method 

- (NSNumber *)numberFromString:(NSString *)string

Given a string which contains a decimal number, such as "1.0" returns NSNumber object with wrong value. 

Steps to Reproduce:

1. Create a new NSNumberFormatter object instance using alloc-init
2. Call the newly created object instance's numberFromString: method and provide it with an NSString argument which has a decimal number, for instance "1.0"
3. Log the result of newly created NSNumber's intValue method into the console: NSLog(@"%d", [number intValue]);

Expected Results:

Console logs the number representation of given string, in this case, the console output should be "1"

Actual Results:

The console output is "0"

Notes:

I tested this on iOS 4.3 and 5.1 devices and the results are the same. But when I tested it on iOS Simulator with iOS 5.1, the result was correct which is somewhat confusing. There's a sample piece of code in attachment which reproduces the bug on the device. Right now, the only workaround this is to use NSScanner.

09-May-2012 09:52 PM Michal Tuszynski:
'number_formatter.m' was successfully uploaded

//Contents of number_formatter.m:
NSString *sampleString = @"1.0";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *number = [formatter numberFromString:sampleString];
    
//The output in console should be 1, but is 0
NSLog(@"%d", [number intValue]);

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!