6

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.

Kurt
  • 855
  • 2
  • 12
  • 22
  • You know, I think we are answering this wrong. We are taking a string and converting it to a number and then back to a string?? And I think what is wanted is a number. Can you please tell us if you actually want a string or a number. And what is it starting from?? You code and question are a bit skewed. Cheers – markhunte Jan 15 '12 at 23:07
  • @markhunte I noticed the same thing so I provided optional implementations for creating the `NSNumber`. There is also the question of `NSNumber` is even necessary so I provided optional implementations with out using `NSNumber`. – zaph Jan 15 '12 at 23:24
  • @markhunte The input is from a UITextField. It is for sizes that are described as:0000,000,00,0,1,2,3...as well as 4.5,5.25,etc. The CoreData type is float and the property is NSNumber (because I want to be able to sort it in a grouped table,numerically:0,1,2,11,23 not 0,1,11,2,23). It is displayed in the table cell as text. The conversion is from string to number to string. Dumb I know. However, NSNumericSearch doesn't seem to work for iOS, at least for me. I had been ignoring 000 in the past, but some clients want that ability. – Kurt Jan 16 '12 at 05:46

4 Answers4

8

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];
zaph
  • 111,848
  • 21
  • 189
  • 228
  • I know the 000's are useless. It is not a value, it is a descriptor of size. The input field may have: 0000,000,00 or integers or floats. I think I may have to do this differently. I may have to use strings and set up a custom sort, although I tried that with NSNumericSearch without joy. – Kurt Jan 16 '12 at 05:52
1

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.

Nikso
  • 1,124
  • 8
  • 7
1

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);
markhunte
  • 6,805
  • 2
  • 25
  • 44
  • Cheers, I was only doing it for the 000 case. But at the last minute spur of the moment before posting tried it with 45 instead of 4.5 by mistake. Thats why I added about it working for both numbers. But as you say it does not which was not my real intention anyway. – markhunte Jan 15 '12 at 23:34
0

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

Alex Zavatone
  • 4,106
  • 36
  • 54