Search code examples
ioslocalecllocationnslocalecllocationcoordinate2d

Are CLLocationCoordinates lat/lon values Locale Dependent?


I turn CLLocations into comma-separated strings such as "75.45874, -45.17292" simply by using an [NSString stringWithFormat:@"%.5f, %.5f"]; and send them to my server application, which expects the string as two numbers with decimal points as the decimal separator.

After reading through all the docs, I can't for the life of me figure out if those coordinates are locale dependent or not, i.e. if a user was overseas (I'm in the US) would their location have a comma as the decimal separator: "75,45874, -45,17292"?

I know a few different ways that I could translate the comma to a decimal if needed, like using an NSNumberFormatter, I'm just trying to figure out if I'd actually need to or not? I've tried simulating locations with the simulator, but I don't know if the locale settings would trickle down to a CLLocation's latitude/longitude properties and would properly emulate such a situation. Any insights?


Solution

  • The coordinates in a CLLocation are represented as CLLocationDegrees which is defined as double. This are floating point numbers and not locale dependent.

    Creating a string with

    [NSString stringWithFormat:@"%.5f, %.5f", lat, lon]
    

    is also not locale dependent. stringWithFormat (almost) conforms to the IEEE printf specification which defines the %f conversion as

    The double argument shall be converted to decimal notation in the style "[-]ddd.ddd", ...

    So your code will always produce a point as decimal separator.

    On the other hand, NSNumberFormatter uses the user's locale preferences for the conversion unless specified otherwise. You can use it in your case if you set the locale to the standard "POSIX locale":

    NSNumberFormatter *myFormatter = [[NSNumberFormatter alloc] init];
    [myFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];