Error with NSNumberFormatter currencySymbol

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


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 currencySymbol, as it can change.
Is there a way to get the correct currencySymbol corresponding to this currency code?

thanks

+6
source share
4 answers

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

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

To get a 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 currencyCode code (JPY, USD, EUR, ..) that interests you.

+2
source

The problem may be that [formatter setLocale:[NSLocale currentLocale]]; overwrites some of the settings you did before. You can try moving this line to the beginning (immediately after the line alloc / init).

+1
source

Try this formatter.currencyCode = @ "USD"; Provide the desired currency code here, it will return the correct character

+1
source

I used the following code / methods to get the currency from the 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]; } 
-1
source

Source: https://habr.com/ru/post/906196/


All Articles