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.
What you're looking to get rid of is the Currency Symbol.
[price setCurrencySymbol:@""];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With