R – Persisting Currency format in Cocoa on a device that so easily could change Region Format


I'm storing my currency values in NSDecimalNumbers (as recommended in multiple other threads on this forum) and using an NSNumberFormatter to display them correctly and handle the text input. All good! However, when I change the Region Format under iPhone prefs General settings to somewhere outside the US, say Germany, and then look at my app ALL my numbers have changed from $ to €. So an amount that was originally entered as "$100.00" is now displayed as "100,00 €".

This is bad. Because, sadly, the dollar is no longer valued the same as the Euro – not even close 🙁 But, I digress… Ultimately, I'm not looking for conversion. I'd simply like to display the amount in the currency it was originally created with.

What is the Cocoa way to go about ensuring that these numbers format correctly as users travel the globe? I'd hate to have to limit the usefulness of my app to a single currency…

Best Solution

When entering values you can get the current system ISO 4217 currencyCode and store it with the number (it's just a string):

NSString* currencyCode = [[[[NSNumberFormatter alloc] init] autorelease] currencyCode];

When displaying data, set this currencyCode on your NSNumberFormatter:

// NSNumberFormatter* formatter = [[[[NSNumberFormatter alloc] init] autorelease] setCurrencyCode:currencyCode];

Edit for typo in code:

NSNumberFormatter* formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setCurrencyCode:currencyCode];