Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i've run into quite an issue with localizedStringWithFormat running iPhone OS 3.1.

Code:
NSString *size = [NSString localizedStringWithFormat:@"%f", 1.53489];
NSLog(@"%.2f", [size floatValue]);

if you run the above code with the device's regional format set to United States, you get the following output:

Code:
1.53

but if you run the above code with the device's regional format set to French, you get the following output:

Code:
1.00

shouldn't it display 1,53 (note the comma)?! where is 1.00 coming from?!
 
I think this is the expected behavior. The first line is parsing the float number into a string, and when the region format is set to French you get a string containing the text "1,53489".

The second line has problems. The floatValue method of NSString doesn't understand region formats, so when it tries to convert the string into a float value it stops on the first unrecognized character which is the comma. The result should be "1.00".
 
I think this is the expected behavior. The first line is parsing the float number into a string, and when the region format is set to French you get a string containing the text "1,53489".

The second line has problems. The floatValue method of NSString doesn't understand region formats, so when it tries to convert the string into a float value it stops on the first unrecognized character which is the comma. The result should be "1.00".

ah! i see now! well, this sucks. and i suppose there is no such thing similar to localizedFloatValue?

[EDIT] NSScanner will do... too bad, localizedFloatValue would be a nice addition to the SDKs

Code:
NSString *size = [NSString localizedStringWithFormat:@"%f", 1.53489];
NSLog(size);
float theLocalizedFloat = [size floatValue];
[[NSScanner localizedScannerWithString:size] scanFloat:&theLocalizedFloat];
NSLog(@"%f", theLocalizedFloat);
NSString *theString = [NSString localizedStringWithFormat:@"%.2f", theLocalizedFloat];
NSLog(theString);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.