I use the following code to format string numbers by adding thousand separation points:
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:@"0.000"];
NSLog(@"decimalNumber = %@",decimalNumber);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:@"."];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:@","];
[numberFormatter setMaximumFractionDigits:9];
NSString *finalString = [numberFormatter stringFromNumber:decimalNumber];
NSLog(@"finalString = %@",finalString);
And if I want to format @"0.000" string (it's not necessary to be 3 zeros, there can be less or much more) I have the following log output:
decimalNumber = 0
finalString = 0
but what I want is to keep fraction zeros, i.e in this case my string must remain unchanged. I see that these zeros are lost when I create the NSDecimalNumber
, and I need another approach. What can be it?
Add this:
[numberFormatter setMinimumFractionDigits:3];