NSNumberFormatter 货币符号错误

发布于 2024-12-27 08:09:15 字数 1417 浏览 2 评论 0原文

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

我无法强制货币符号,因为它可以更改。
有没有办法获得与给定的currencyCode对应的正确的currencySymbol?

谢谢

I have a problem getting the currencySymbol of my NSNumberFormatter.
I use a NSNumberFormatter with a currency code "EUR".
When I format prices, the symbol is correct, I get the € symbol.
However, when I want to get just the currencySymbol with the method [formatter currencySymbol], the symbol $ is returned.
If I manually set the currencySymbol (with "A" for instance) everything will work fine and the method [formatter currencySymbol] will return the "A" symbol.

Here is my code

// 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]]);

Here are the console results :

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

I cannot force the currencySymbol since it can change.
Is there a way to get the right currencySymbol corresponding to a given currencyCode ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反差帅 2025-01-03 08:09:15

要获取货币符号,您可以将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、 ..) 你感兴趣。

To get the currency symbol you can use NSLocaleCurrencySymbol with NSLocale:

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

To get the localized name ('US Dollar', 'Japanese Yen', ..) you can use NSLocaleCurrencyCode:

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

where currencyCode is the currency code (JPY, USD, EUR, ..) you're interested in.

心奴独伤 2025-01-03 08:09:15

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

The issue might be that [formatter setLocale:[NSLocale currentLocale]]; overwrites some of the configuration you did prior to it. You may want to try movig that line to the top (just after the alloc/init line).

羁绊已千年 2025-01-03 08:09:15

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

Try this formatter.currencyCode = @"USD";
Provide the desired Currency Code here it will return proper symbol

安静 2025-01-03 08:09:15

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

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

i have used the following code/methods to get the currency from a float value

 +(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];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文