6

我在获取 NSNumberFormatter 的 currencySymbol 时遇到问题。
我使用带有货币代码“EUR”的 NSNumberFormatter。
当我格式化价格时,符号是正确的,我得到了€符号。
但是,当我想使用方法 [formatter currencySymbol] 获取 currencySymbol 时,会返回符号 $。
如果我手动设置currencySymbol(例如使用“A”),一切都会正常工作,方法[formatter currencySymbol] 将返回“A”符号。


这是我的代码

// Create formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:@"EUR"];
[formatter setLocale:[NSLocale currentLocale]];

// Log the currency symbol
NSLog(@"[formatter currencyCode] : %@", [formatter currencyCode]);
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);
[formatter setCurrencySymbol:@"A"];
NSLog(@"[formatter currencySymbol] : %@", [formatter currencySymbol]);
NSLog(@"[formatter currencySymbol] : %@", [formatter stringFromNumber:[NSNumber numberWithInt:0]]);

以下是控制台结果:

2012-01-17 12:29:11.108[4545:207] [formatter currencySymbol] : $
2012-01-17 12:29:11.109[4545:207] [formatter currencySymbol] : €0.00
2012-01-17 12:29:11.110[4545:207] [formatter currencySymbol] : A
2012-01-17 12:29:11.111[4545:207] [formatter currencySymbol] : A0.00

我不能强制使用currencySymbol,因为它可以改变。
有没有办法获得与给定 currencyCode 对应的正确 currencySymbol ?

谢谢

4

4 回答 4

2

要获取您可以使用的货币符号NSLocaleCurrencySymbolNSLocale

   NSLocale* locale = [NSLocale currentLocale];
   NSString* cs = [locale displayNameForKey:NSLocaleCurrencySymbol
                                 value:currencyCode];

要获取本地化名称(' US Dollar '、' Japanese Yen '、..),您可以使用NSLocaleCurrencyCode

   NSLocale* locale = [NSLocale currentLocale];
   NSString* cc = [locale displayNameForKey:NSLocaleCurrencyCode
                              value:currencyCode];

currencyCode感兴趣的货币代码(JPY、USD、EUR、..)在哪里。

于 2014-05-05T16:28:25.750 回答
1

问题可能是[formatter setLocale:[NSLocale currentLocale]];覆盖了您之前所做的一些配置。您可能想尝试将该行移动到顶部(就在 alloc/init 行之后)。

于 2012-01-17T13:16:09.873 回答
0

试试这个 formatter.currencyCode = @"USD"; 在此处提供所需的货币代码,它将返回正确的符号

于 2015-11-24T06:28:15.957 回答
-1

我使用以下代码/方法从浮点值中获取货币

 +(NSString *) formatCurrencyTypeFloat: (float )val {

 NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"us_ES"];
    or
    NSLocale *locale = [NSLocale currentLocale];;
    [formatter setLocale:locale]; 
    NSLog(@"%@",[formatter stringFromNumber:[NSNumber numberWithFloat:val]]);
    return [formatter stringFromNumber:[NSNumber numberWithFloat:val]];

}



+(NSString *) formateCurrencyTypeFloat:(float)val withSign:(NSString *)currencySign{
    NSNumberFormatter * fmt;
    NSNumber          * n;

    fmt = [[ [ NSNumberFormatter alloc ] init ]autorelease];
    n   = [ NSNumber numberWithFloat: val];

    [ fmt setFormatterBehavior: NSNumberFormatterBehavior10_4 ];
    [ fmt setCurrencySymbol: currencySign ]; //==> currencySign can be = @"$";
    [ fmt setNumberStyle: NSNumberFormatterCurrencyStyle ];


    return [ fmt stringFromNumber:n];
}

+(NSString *) formateCurrencyTypeFloat:(float)val withCode:(NSString *)currencyCode{
        NSNumberFormatter * fmt;
        NSNumber          * n;

        fmt = [[ [ NSNumberFormatter alloc ] init ]autorelease];
        n   = [ NSNumber numberWithFloat: val];

        [ fmt setFormatterBehavior: NSNumberFormatterBehavior10_4 ];
        [fmt setCurrencyCode:currencyCode];//====>@"USD";
        [ fmt setNumberStyle: NSNumberFormatterCurrencyStyle ];


        return [ fmt stringFromNumber:n];
    }
于 2012-07-12T12:33:54.903 回答