确定一种货币的十进制数

时间:2022-11-07 16:31:03

On the iPhone:

在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 has no decimals and this impacts my custom keyboard significantly. I'm trying to find a method in the Cocoa-touch framework which will tell me how many decimal places a specific currency has - my hard-coded value of 2 isn't doing me justice :(

日本货币没有小数,这对我的自定义键盘影响很大。我正在尝试在可可触摸框架中找到一种方法,它可以告诉我一种特定货币有多少个小数点——我的2的硬编码值并不能说明我的意思:

Can anyone help?

谁能帮忙吗?

4 个解决方案

#1


2  

You should be able to use the CFNumberFormatter objects to get the information you need. Specifically you could use CFNumberFormatterGetDecimalInfoForCurrencyCode:

您应该能够使用CFNumberFormatter对象来获取所需的信息。具体来说,您可以使用CFNumberFormatterGetDecimalInfoForCurrencyCode:

CFStringRef localeIdent = CFSTR("JPY");

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

I hope that helps.

我希望有帮助。

#2


2  

I haven't programmed in Cocoa for ages, but from the documentation for NSNumberFormatter, there's a function called 'currencyDecimalSeparator' - that might at least tell you if a currency has one at all, which might be a start?

我已经很久没有使用Cocoa编程了,但是从NSNumberFormatter的文档来看,有一个叫做“currencyDecimalSeparator”的函数——它至少可以告诉你一种货币是否有,这可能是一个开始?

#3


0  

Financial companies maintain databases of this kind of information. You might be able to buy the data or import it from an online source.

金融公司维护这类信息的数据库。您可能可以购买数据或从在线源导入数据。

Note also: some currencies need three or four decimal places. See http://www.londonfx.co.uk/ccylist.html for examples.

注意:有些货币需要三到四位小数。请参阅http://www.londonfx.co.uk/ccylist.html的例子。

#4


0  

I had a similar problem but the answers above didn't really solve my problem.

我也有类似的问题,但是上面的答案并不能真正解决我的问题。

I ended up using the maximumFractionDigits method on NSNumberFormatter which gave me 0 for Japanese locale. Make sure you use a NSNumberFormatterCurrencyStyle for the formatter, otherwise you'll see decimal places in other formatters.

最后我使用了NSNumberFormatter上的maximumfractionnumbers方法,它为日语语言环境提供了0。确保您对格式化程序使用了NSNumberFormatterCurrencyStyle,否则您将在其他格式化程序中看到小数部分。

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

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

#1


2  

You should be able to use the CFNumberFormatter objects to get the information you need. Specifically you could use CFNumberFormatterGetDecimalInfoForCurrencyCode:

您应该能够使用CFNumberFormatter对象来获取所需的信息。具体来说,您可以使用CFNumberFormatterGetDecimalInfoForCurrencyCode:

CFStringRef localeIdent = CFSTR("JPY");

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

I hope that helps.

我希望有帮助。

#2


2  

I haven't programmed in Cocoa for ages, but from the documentation for NSNumberFormatter, there's a function called 'currencyDecimalSeparator' - that might at least tell you if a currency has one at all, which might be a start?

我已经很久没有使用Cocoa编程了,但是从NSNumberFormatter的文档来看,有一个叫做“currencyDecimalSeparator”的函数——它至少可以告诉你一种货币是否有,这可能是一个开始?

#3


0  

Financial companies maintain databases of this kind of information. You might be able to buy the data or import it from an online source.

金融公司维护这类信息的数据库。您可能可以购买数据或从在线源导入数据。

Note also: some currencies need three or four decimal places. See http://www.londonfx.co.uk/ccylist.html for examples.

注意:有些货币需要三到四位小数。请参阅http://www.londonfx.co.uk/ccylist.html的例子。

#4


0  

I had a similar problem but the answers above didn't really solve my problem.

我也有类似的问题,但是上面的答案并不能真正解决我的问题。

I ended up using the maximumFractionDigits method on NSNumberFormatter which gave me 0 for Japanese locale. Make sure you use a NSNumberFormatterCurrencyStyle for the formatter, otherwise you'll see decimal places in other formatters.

最后我使用了NSNumberFormatter上的maximumfractionnumbers方法,它为日语语言环境提供了0。确保您对格式化程序使用了NSNumberFormatterCurrencyStyle,否则您将在其他格式化程序中看到小数部分。

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

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