Is it possible to derive currency symbol from currency code?

前端 未结 5 1613
鱼传尺愫
鱼传尺愫 2021-02-04 19:04

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 cod

5条回答
  •  天涯浪人
    2021-02-04 19:43

    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];
    }
    

提交回复
热议问题