Major Bug? (NSLocalizedStringWithFormat)

Discussion in 'iOS Programming' started by Darkroom, Sep 12, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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?!
     
  2. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    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".
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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);
    
     

Share This Page