NSNumberFormatter currencySymbol出错

时间:2012-01-17 11:47:24

标签: ios currency nsnumberformatter

我在获取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 个答案:

答案 0 :(得分:2)

要获取货币符号,您可以将NSLocaleCurrencySymbolNSLocale一起使用:

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

要获取本地化名称(' 美元',' 日元',..),您可以使用NSLocaleCurrencyCode

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

其中currencyCode是您感兴趣的货币代码(JPY,USD,EUR,..)。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:-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];
    }