Abhinav
Abhinav

Reputation: 38162

Localizing price details -- E.G. $50 Vs RMB 1000

I want to localize the price in my iPhone app. Can you please guide me how to achieve this.

For example:

$50 Vs A$ 1,480.00 Vs RMB 1450 Vs £999.00 etc.

Upvotes: 0

Views: 143

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243156

You have two problems:

  1. How to convert a number in one currency to another currency, especially since currency exchange rates fluctuate hourly.
  2. How to format a number in a specific currency format.

The second one is fairly simple, and @coneybeare answered it in his post: use an NSNumberFormatter.

The first part is also fairly simple, if you know where to look. I recommend checking out a unit converter library I wrote called DDUnitConverter: https://github.com/davedelong/DDUnitConverter

One of the conversion modules it has is a currency converter, which asynchronously pulls it's rates from the International Monetary Fund.

Upvotes: 2

coneybeare
coneybeare

Reputation: 33101

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
NSString *formattedString = [numberFormatter stringFromNumber:price];

NSLog(@"Local price: %@", formattedString);

if you just want the currency symbol, you can use:

NSString *sym = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]

Upvotes: 4

Related Questions