2009-06-26 59 views
2

我正在測試我的應用。除了當我將語言環境更改爲德國時,所有工作都正常。本地化iphone應用貨幣

基本上你用你的本地貨幣輸入2個值,計算髮生,用戶獲得信息。

用戶數字輸入處理得很好。也就是說,在「Editing Did End」上執行一個方法,將該數字轉換爲等值的本地貨幣。所以如果美國用戶輸入10000,他們將被退回$ 10,000.00。代碼如下:

- (NSMutableString *) formatTextValueToCurrency: (NSMutableString *) numberString { 


NSNumber *aDouble = [NSNumber numberWithFloat: [numberString floatValue]]; 
NSMutableString *aString = [NSMutableString stringWithCapacity: 20]; 
NSLocale *theLocale; 


NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 

[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 
theLocale = [NSLocale currentLocale]; 
[currencyStyle setLocale: theLocale]; 

[aString appendString: [currencyStyle stringFromNumber:aDouble]]; 

[currencyStyle release]; 

return aString; 

} 

但是,當我想處理上述貨幣值以獲取用戶他/她的信息時發生問題。也就是說,應用程序現在需要從$ 10,000.00(或任何貨幣)中獲得10000以發送到計算方法中。這裏是代碼:

- (float) getValueFromCurrency: (id) sender { 

NSNumber *aDouble = [NSNumber numberWithFloat: 0.0]; 
UITextField *textField = (UITextField *) sender; 
NSMutableString *aString= [NSMutableString stringWithCapacity: 20]; 
NSLocale *theLocale; 

float result; 

NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];  

[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

theLocale = [NSLocale currentLocale]; 

[currencyStyle setLocale: theLocale]; 
NSLog(@"The locale is %@", currencyStyle.locale.localeIdentifier); 
//Above NSLog looks good because it returns de_DE 

[aString appendString: textField.text]; 
//The append from text field to string is good also 

aDouble = [currencyStyle numberFromString: aString]; 
//For some reason, nil is returned 

result = [aDouble floatValue]; 

[currencyStyle release]; 

return result; 
} 

由於某些原因,美國,英國,日本和愛爾蘭的語言環境都很好。

歐洲大陸國家無法使用。

任何關於如何解決這個問題的建議將會很棒。

回答

2

鑑於代碼適用於美國,英國,日本和愛爾蘭,但不是歐洲大陸(德國),我會檢查你是如何處理數千和十進制分隔符的。

也就是說,在爲你的代碼工作的國家裏,千位分隔符是逗號,小數點就是點(句號)。所以10000美元和50美分就是10,000.50美元。

在歐洲大陸(德國等),千分位是點(句號),小數點位置是逗號。因此,在德國上面的值,將10.000,50

NSNumberFormatter有兩個方法,你可能想看看: -

  • (的NSString *)currencyDecimalSeparator
  • (的NSString *)currencyGroupingSeparator

對於每種格式,您都可以在WIKI(http://en.wikipedia.org/wiki/Decimal_separator)中找到一個國家/地區的列表,然後測試以確定您的問題是否僅針對一個羣組。

我希望有幫助。

乾杯,

凱文