Question

the following code doesn't work as intended:

//[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
formatter.maximumFractionDigits = 2;
formatter.minimumFractionDigits = 2;
formatter.decimalSeparator = @",";
formatter.perMillSymbol = @".";
NSString *resultText = [formatter stringFromNumber:[NSNumber numberWithFloat:1234.10]];

The result string is then 1234,10, but the perMillSymbol is ignored. When I uncomment the setNumberStyle call, the result is 1,234,10 - ignoring my set perMillSymbol.

What's wrong here?

Was it helpful?

Solution

If you're trying to format the number as 1.234,10, try this:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
formatter.maximumFractionDigits = 2;
formatter.minimumFractionDigits = 2;
formatter.decimalSeparator = @",";
formatter.groupingSeparator = @".";
NSString *resultText = [formatter stringFromNumber:[NSNumber numberWithFloat:1234.10]];

The per mill symbol is used to symbolise one tenth of a percent (0.001) and is not relevant to a decimal formatted number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top