NSNumberFormatter localized setGroupingSeparator?

With NSNumberFormatter, it is very easy to format a number using a fixed string like GroupingSeparator.

[numberFormatter setGroupingSeparator:@"."]; 

But in some countries @ "." is not a valid delimiter: instead of it, instead of @@ "," (comma). How can I get a localized grouping separator from the system so that my formatting is correct worldwide in any language?

+4
source share
1 answer

You can use setLocale: ::

 [numberFormatter setLocale:[NSLocale currentLocale]]; 

The locale provides the correct characters for things like decimal separator, quotation marks, and units of measure and currency.

But! Beware of using this for currency; there is no automatic conversion of values. If you have a number representing a certain amount in US dollars, and just format it as if it were euros, it will be formatted correctly, but will not have the correct value.

+7
source

All Articles