Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone -> NSNumberFormatter - price without the currency code

I have an iPhone app and I want to display a price. So I use these lines of code:

NSNumberFormatter *price = [[NSNumberFormatter alloc] init];
[price setNumberStyle:NSNumberFormatterCurrencyStyle];
[price setCurrencyCode:@"USD"];

What I want is only the price, formatted for the currencyCode but in my case without USD. So instead of 30,00 $, I want to have only 30,00 without $.

How can I do this?

Best Regards, Tim.

like image 627
Tim Avatar asked Feb 18 '11 16:02

Tim


3 Answers

What you're looking to get rid of is the Currency Symbol.

[price setCurrencySymbol:@""];
like image 198
SomeRandomGuy Avatar answered Oct 29 '22 03:10

SomeRandomGuy


You can use the setCurrencySymbol to an empty string which should do the trick:

[price setCurrencySymbol:@""];

Please keep in mind that this only sets the local currency code and it might not work with foreign currencies.

like image 45
Andy S. Avatar answered Oct 29 '22 02:10

Andy S.


Here's a way to do it while keeping the correct locale formatting for the symbol:

_fmt.positiveFormat = [_fmt.positiveFormat 
    stringByReplacingOccurrencesOfString:@"¤" withString:@""];
_fmt.negativeFormat = [_fmt.negativeFormat 
    stringByReplacingOccurrencesOfString:@"¤" withString:@""];

The ¤ character is used in the currency formats for the currency symbol. If you replace the currency symbol as suggested in some of the other answers, the formatter does a US standard format for the number (AFAIK - tested in iOS 4 and 5 in the USA). You'll need to do this replacement after setting the locale for the formatter. Try with the currency code HUF which does not have pennies.

like image 1
Marshall Weir Avatar answered Oct 29 '22 03:10

Marshall Weir