NSNumberFormatter keep decimal separator even when no decimals are present

178 Views Asked by At

I have a UILabel and its formatter :

 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

[formatter setGroupingSeparator:@"."];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];
[formatter setDecimalSeparator:@","];

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:12.]]];

So that my formattedString is returning 12

What If I want to keep the comma when there is no decimal? So that my UIlabel prints out "12."?

1

There are 1 best solutions below

0
Rob On

If you want to see comma when there is no decimal, set alwaysShowsDecimalSeparator accordingly

[formatter setAlwaysShowsDecimalSeparator:YES];

Thus, your UILabel will say "12," (not "12.").