NSNumberFormatter leading 0 and decimal places

Is there a way to format NSNumber with leading 0 and decimal places? For example, I need to be able to write 4.5, as well as 000. I currently have it, where it will give decimals, but it does not lead 0.

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 above is "myString: 4.5" and "myOtherString: 0". I need to be able to do both "4.5" and "000" as output.

I watched the "Apple Data Formatting Guide" without much success.

+6
source share
4 answers

Note that [f numberFromString:@"4.5"] returns 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 do not have lines to start, just create a number, for example:

 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 do not need an NSNumber , just use standard formatting:

 myString = [NSString stringWithFormat:@"%.1f", 4.5]; myString = [NSString stringWithFormat:@"%03d", 0]; 
+6
source

You can try something like:

 NSString *myString = [NSString stringWithFormat:@"%03f", [myNSNumber floatValue]]; 

This, following printf , will print your number, forcing you to print at least 3 digits and filling in "0 any empty space."

+1
source

How about this as a variation of the theme for 000

  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); 
+1
source

Since it is often asked questions, and Apple docs suck, this is the answer that people will look for. The link below has two solutions. One uses an NSString stringWithFormat: and the other NSNumberFormatter .

fooobar.com/questions/531143 / ...

0
source

Source: https://habr.com/ru/post/906054/


All Articles