Invalid output when using currency style using NSNumberFormatter

I'm currently trying to format a float from a JSON feed to a number using NSNumberFormatter using a currency style.

My current solution is this:

float test = 100.50; NSNumber *number = [NSNumber numberWithFloat:test]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [formatter setCurrencyCode:@"DKK"]; NSString *price = [formatter stringFromNumber:number]; 

My problem is that the output is "DKK100.50" when the correct result for the selected currency should be "DKK100.50" (note that the current result is with a dot as a decimal separator, and not as a comma).

What am I doing wrong?

+4
source share
1 answer

You do not specify NSLocale for your NSNumberFormatter , it will use the current "region" in the setting to configure the currency format (including the separator). Your current "region" setting may be a region in which a dot is used as a separator, change it to one that uses a comma, for example en_US

 [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]]; 

eg

+1
source

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


All Articles