initWithFormat won't use locale for decimal point

Discussion in 'iOS Programming' started by RLScott, Jul 17, 2012.

  1. RLScott macrumors newbie

    Joined:
    Jul 17, 2012
    Location:
    Minnesota
    #1
    My app displays decimal numbers and I would like it to display in the user's locale so that the decimal point is either a period "." or a comma "," depending on the user's device locale. When I set the device locale to Germany and call localizedStringWithFormat I get the comma "," decimal point as expected. But when I call initWithFormat I always get the period decimal point ".". The docs say that initWithFormat is supposed to follow the user's device locale. I know the device locale is set correctly because localizedStringWithFormat works properly. What am I doing wrong? I have tested this on the iPhone 4.0 Simulator and on a iPhone 3 real device. I am using XCODE 4.2, build 4C199.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #2
    Post your code.


    From the description for localizedStringWithFormat:
    This method is equivalent to using initWithFormat:locale: and passing [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] as the locale argument.

    From the description for initWithFormat:
    Invokes initWithFormat:locale:arguments: with nil as the locale, hence using the canonical locale to format numbers. This is useful, for example, if you want to produce "non-localized" formatting which needs to be written out to files and parsed back later.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    From the docs:

    So, if you want to use initWithFormat: and a locale, you'll need to call initWithFormat:locale:
     
  4. RLScott thread starter macrumors newbie

    Joined:
    Jul 17, 2012
    Location:
    Minnesota
    #4
    Ah, I misread the docs. So initWithFormat is not supposed to follow the user's locale. I guess I'll either switch over to localizedStringWithFormat or initWithFormat:...locale: [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]. Thanks.
     

Share This Page