[Solved] NSNumberFormatter thousand separator and trailing zeros


An array balance is an array of NSString NOT NSNumbers. That’s why method stringFromNumber: returns nil.

The following example works fine:

NSArray *balanceArr =@[@(120.50), @(8500.00)];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:2];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:@"'"];
for (NSNumber *balance in balanceArr) {
    NSLog(@"Straight printing: %@", balance);
    NSLog(@"NumberFormatter: %@", [formatter stringFromNumber:balance]);
}

Output:

2014-08-22 12:18:16.669 dsghdtu[5343:60b] Straight printing: 120.5
2014-08-22 12:18:16.671 dsghdtu[5343:60b] NumberFormatter: 120.50
2014-08-22 12:18:16.671 dsghdtu[5343:60b] Straight printing: 8500
2014-08-22 12:18:16.672 dsghdtu[5343:60b] NumberFormatter: 8'500.00

2

solved NSNumberFormatter thousand separator and trailing zeros