簡體   English   中英

NSNumberFormatter貨幣符號

[英]NSNumberFormatter currency symbol

我的應用程序允許用戶選擇各種貨幣進行支出跟蹤。 我有一個標簽,顯示帶有當前符號的金額。 我正在使用NSNumberFormatterkCFNumberFormatterCurrencyStyle來格式化數量字符串並將其顯示在標簽中;

numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = kCFNumberFormatterCurrencyStyle;
numberFormatter.currencyCode = @"EUR";

我的目標是顯示不同顏色的貨幣符號,所以我使用NSAttributedString,嘗試查找符號的范圍並為其設置不同的屬性。 問題是格式化程序在我初始化屬性字符串時返回錯誤的符號:

MLOG(@"internationalCurrencySymbol %@", numberFormatter.internationalCurrencySymbol);
MLOG(@"currencySymbol %@", numberFormatter.currencySymbol);
MLOG(@"currencyCode %@", numberFormatter.currencyCode);

//logs:
//USD
//$
//EUR

但當標簽顯示在屏幕上時,我看到正確的歐元貨幣符號:

有誰知道如何獲得給定貨幣代碼的貨幣符號?

我剛剛測試了你的代碼。 如果在按照您的方式定義數字格式化之后立即運行日志語句,則輸出為

@"EUR"
@"€"
@"EUR"

請注意,國際貨幣符號和貨幣符號似乎取決於系統中設置的區域設置。 但您可以輕松更改格式化程序的語言環境,如下所示:

numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];

最好不要設置currencySymbol 然后,如果該地區是泰國( @"th_TH" ),那么你就得到了

numberFormatter.internationalCurrencySymbol   "THB"
numberFormatter.currencySymbol                "฿"
numberFormatter.currencyCode                  "THB"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM