Is it possible to derive currency symbol from currency code?

混江龙づ霸主 提交于 2019-12-20 10:48:21

问题


My iPhone app formats an NSDecimalNumber as a currency using setCurrencyCode, however another screen displays just the currency symbol. Rather than storing both the currency code and symbol, is it possible to derive the symbol from the code? I thought the following might work, but it just returns the symbol as $:

currencyCode = [dictPrices valueForKey:@"currencyCode"];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setCurrencyCode:currencyCode];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSString *currencySymbol = [numberFormatter currencySymbol];

回答1:


You want to be setting the locale, not the currency code. Then you will get the expected results from the formatters properties (or tweak them). You can also get things like the currency symbol from the locale object itself. So, for example, if you were displaying a number formatted for Japan in JPY:

NSLocale* japanese_japan = [[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"] autorelease];
NSNumberFormatter *fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmtr setLocale:japanese_japan];

Now your number formatter should give you the correct symbols for currency symbol and format according to the rules in the specified locale.




回答2:


You can get number with currency symbol from number with currency code.

This is the way I do:

    NSString *currencyCode = @"HKD";
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setCurrencyCode:currencyCode];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    cell.amountLabel.text = [numberFormatter stringFromNumber: expense.exAmount];



回答3:


The following code will return only the currencySymbol for a given currencyCode

- (NSString*)getCurrencySymbolFromCurrencyCode:(NSString*)currencyCode {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setCurrencyCode:currencyCode];
    [formatter setMaximumFractionDigits:0];
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:0]];
    [formatter release];
    return [string substringFromIndex:1];
}



回答4:


NSNumberFormatter * formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;

NSString * localeIde = [NSLocale localeIdentifierFromComponents:@{NSLocaleCurrencyCode: currencyCode}];
formatter.locale = [NSLocale localeWithLocaleIdentifier:localeIde];

NSString * symbol = formatter.currencySymbol;



回答5:


I would actually do it this way. If you decide to do it as SveinnV said it may be that the symbol is place before the 0 amount and then you are just getting @"0" as the symbol:

- (NSString *) currencySymbolForCurrencyCode:(NSString *) currencyCode
{

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setMaximumFractionDigits:0];

    [formatter setCurrencyCode:currencyCode];

    NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:0]];
    NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];

    return [components firstObject];
}


来源:https://stackoverflow.com/questions/2574191/is-it-possible-to-derive-currency-symbol-from-currency-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!