Question

I'm wondering if there is any official way to put a currency symbol to the left or to the right from the money amount, depending of the region and the currency itself? For example, if it's a $ sign, it should be to the left of the amount, as $20. If it's, for instance, € sign, then it should go as 20€. I've found a simple way, I just check with If statement if the currency symbol is $ and append it to the left or to the right to my NSString. But this looks a bit lame like a crutch method. I was unable to find some more convenient way. Is there any? Thank you.

Was it helpful?

Solution

Apples official way ......

You need to use NSNumbers and NSNumberFormatter to convert the value to a currency and return a string representing it. This can be used in our labels e.t.c. someDouble refers to the number you wish to convert to a currency format.

NSNumber *currencyValue = [NSNumber numberWithDouble:someDouble];
NSNumberFormatter *formater = [[NSNumberFormatter alloc] init];
[formater setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *currencyString = [formater stringFromNumber:currencyValue];

NOTE: you may need to play around with the setLocale to get it to display the correct currency for a given region.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top