Determining the number of decimal numbers for a currency

On iPhone:

Using the US locale, a currency looks like this: $ 1,234.56
Using the UK locale, a currency looks like this: £ 1,234.56
Using the German (Germany) locale, a currency looks like this: € 1.234.56

and finally, a Japanese currency: ¥ 1,234

The Japanese currency does not have decimals, and this greatly affects my user keyboard. I am trying to find a method in the Cocoa -touch structure that will tell me how many decimal places a particular currency has - my hard-coded value 2 does not do me justice :(

Can anyone help?

+3
source share
4 answers

CFNumberFormatter . , CFNumberFormatterGetDecimalInfoForCurrencyCode:

CFStringRef localeIdent = CFSTR("JPY");

int numDecimals;
double rounding;
BOOL result = CFNumberFormatterGetDecimalInfoForCurrencyCode(localeIdent, &numDecimals, &rounding);

, .

+2

Cocoa , NSNumberFormatter , "currencyDecimalSeparator" - , , , , ?

+2

I had a similar problem, but the answers above did not help solve my problem.

I ended up using the maximumFractionDigits method for NSNumberFormatter, which gave me 0 for the Japanese locale. Be sure to use NSNumberFormatterCurrencyStyle for formatting , otherwise you will see decimals in other formats.

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

    NSLog(@"Number of decimal places %i", [currencyFormatter maximumFractionDigits]);
0
source

Source: https://habr.com/ru/post/1712422/


All Articles