2009-07-12 35 views
3

在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 

日本的貨幣沒有小數和這會影響我的自定義鍵盤顯著。我試圖在Cocoa-touch框架中找到一個方法,它會告訴我一個特定貨幣有多少小數點 - 我的硬編碼值爲2並不是我公平的:(

任何人都可以幫忙嗎?

回答

2

您應該可以使用CFNumberFormatter對象來獲取所需的信息。具體來說,你可以使用CFNumberFormatterGetDecimalInfoForCurrencyCode:

CFStringRef localeIdent = CFSTR("JPY"); 

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

我希望幫助。

0

金融公司保持這種信息的數據庫,你也許可以買數據或從在線源導入

還要注意:。某些貨幣需要三個或四個小數位見http://www.londonfx.co.uk/ccylist.html的例子。

+0

NSNumberFormatter非常出色。格式化程序沒有格式化問題,因爲格式化程序都是由我完成的。我只是無法確定如何確定需要預留多少個小數位。感謝鏈接 - 有趣的東西。 – rein 2009-07-12 19:52:37

2

我還沒有在Cocoa中編程多年,但是從NSNum的文檔berFormatter有一個叫做'currencyDecimalSeparator'的函數 - 至少可以告訴你貨幣是否有一個貨幣,這可能是一個開始?

0

我有一個類似的問題,但上面的答案並沒有真正解決我的問題。

我結束了在NSNumberFormatter上使用maximumFractionDigits方法,它給了我日語區域設置0。確保格式化程序使用了NSNumberFormatterCurrencyStyle,否則您將在其他格式化程序中看到小數位。

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

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