Локализуйте Валюту для iPhone

Я полагаю, что ваш код сработает, если вместо этого вы напечатаете Hifact(m,n) в строке 8. Я проверил это, и код напечатал 5 по желанию. Я также проверил с некоторыми другими значениями, которые все еще дали правильный ответ.

16
задан quoo 27 April 2010 в 17:41
поделиться

1 ответ

NSNumberFormatter is definitely the way to go! You can set a NSLocale on the NSNumberFormatter, the formatter will automatically behave according to that locale. The default locale for a number formatter is always the currency for the users selected region format.

NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:@"5.00"];

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSLog(@"%@", [currencyFormatter stringFromNumber:someAmount]);

This will log the amount '5.00' according to the users default region format. If you want to alter the currency you can set:

NSLocale *aLocale = [[NSLocale alloc] initWithLocaleIdentifier: "nl-NL"]
[currencyFormatter setLocale:aLocale];

Which will choose the default currency for that locale.

Often though you're not charging in your user's local currency, but in your own. To force NSNumberFormatter to format in your currency, while keeping the number formatting in the user's preference, use:

currencyFormatter.currencyCode = @"USD"
currencyFormatter.internationalCurrencySymbol = @"$"
currencyFormatter.currencySymbol = @"$"

In en-US this will format as $5.00 in nl-NL it's $ 5,00.

32
ответ дан 30 November 2019 в 17:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: