Reputation: 855
Is there any way to format an NSNumber with leading 0's and decimals? For example, I need to have the ability to write 4.5 as well as 000. Currently I have it where it will allow decimals, but not leading 0's.
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterNoStyle;
NSString *myString = [f numberFromString:@"4.5"];
NSLog(@"myString: %@",myString);
NSString *myOtherString = [f numberFromString:@"000"];
NSLog(@"myOtherString:%@",myOtherString);
The output from above would be: 'myString:4.5' and 'myOtherString:0'. I need to be able to do both '4.5' and '000' as output.
I have looked at Apple's "Data Formatting Guide" without much success.
Upvotes: 6
Views: 8365
Reputation: 4323
Since this is asked often and Apple's docs suck, this is the answer that people will be looking for. The link below has two solutions. One using NSString stringWithFormat:
and the other using NSNumberFormatter
.
https://stackoverflow.com/a/11131497/1058199
Upvotes: 0
Reputation: 6932
How about this as a variation on theme for the 000's
NSNumber *myNumber;
NSString *myString =@"000" ;
NSString * myStringResult;
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterNoStyle;
[f setHasThousandSeparators:FALSE]; //-- remove seperator
[f setMinimumIntegerDigits:[myString length ]]; //-- set minimum number of digits to display using the string length.
myNumber = [f numberFromString:myString];
myStringResult = [f stringFromNumber:myNumber];
NSLog(@"myStringResult: %@",myStringResult);
Upvotes: 1
Reputation: 112857
Note that [f numberFromString:@"4.5"]
returns an NSNumber*
not a NSString*
You want something like this:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterNoStyle;
NSNumber *myNumber;
NSString *myString;
myNumber = [f numberFromString:@"4.5"];
[f setNumberStyle:kCFNumberFormatterDecimalStyle];
myString = [f stringFromNumber:myNumber];
NSLog(@"myString: %@",myString);
myNumber = [f numberFromString:@"000"]; // Note that the extra zeros are useless
[f setFormatWidth:3];
[f setPaddingCharacter:@"0"];
myString = [f stringFromNumber:myNumber];
NSLog(@"myString: %@",myString);
NSLog output:
myString: 4.5
myString: 000
If you don't have strings to start with just create number like:
myNumber = [NSNumber numberWithFloat:4.5];
myNumber = [NSNumber numberWithInt:0];
Or just use standard formatting:
myString = [NSString stringWithFormat:@"%.1f", [myNumber floatValue]];
myString = [NSString stringWithFormat:@"%03d", [myNumber intValue]];
Or if you don't need an NSNumber
representation just use standard formatting :
myString = [NSString stringWithFormat:@"%.1f", 4.5];
myString = [NSString stringWithFormat:@"%03d", 0];
Upvotes: 8
Reputation: 1124
You could try something like:
NSString *myString = [NSString stringWithFormat:@"%03f", [myNSNumber floatValue]];
This, following the printf format, will print your number forcing at least 3 digits to be printed and padding with '0's any empty space.
Upvotes: 1