Currency formatting example is incorrect for some currencies


The example in the book that says to do the following with regards to currency “symbols” does not accurately use the Locale information:
NSString *currencySymbol = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
[valueLabel setText:[NSString stringWithFormat:@"%@%d", currencySymbol, [possession valueInDollars]]];

It assumes that the currency “symbol” should be prepended with no space or other character in between, but the Locale format allows for specifying that the currency “symbol” is suffixed and can have space in between. For example, the Swedish Krona should be represented as “123 kr”, not “kr123” as the example above results in. Using the NSNumberFormatter class as in the following code does what the Locale settings intend. As a side benefit you also get thousands (or whatever the locale setting dictates) separators inserted appropriately:

NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
[nf setNumberStyle:NSNumberFormatterCurrencyStyle];
[nf setMaximumFractionDigits:0];  // optional, but probably right for this app
[valueLabel setText:[nf stringFromNumber:[NSNumber numberWithInt:[possession valueInDollars]]]];

To avoid truncation of your localized numbers, you need to increase the hard-coded 40-pixel space on the right in the list view. It would be nice to auto-size it based on a minimum “amount” you’d like to be able to display based on the current locale but I didn’t search for a solution to that (e.g. measure a currency-formatted version of 9999 in the view font and use that measurement to set up all your views).

Curious note relative to Mac OS X: it seems the locale settings are different between Mac and iOS when it comes to currencies; it looks like they all are prefixed and they’ve added characters to disambiguate (such as SKr for “Swedish Krona” – which as far as I can tell is non-standard and they should probably just have used the ISO 4217 three-letter currency codes or added an option in Numbers to choose between standard code and Locale prettiness)