English 中文(简体)
NSNumberFormatter leading 0 s and decimals
原标题:NSNumberFormatter leading 0 s and decimals

是否有办法以零头和小ci为主编NSNumber? 例如,我需要有能力撰写4.5和000字。 目前,我在那里会允许诽谤,但不会领导零。

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);

上述产出是:我的缩略语:4.5和我的其他成员:0。 作为产出,我需要能够做到4.5和000。

我对“催化格式指南”进行了研究,但没有取得多大成功。

问题回答

注:[f numberFromString:@“4.5”] 回归代码>。 NSNumber* not a NSString*

You:

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);

安保记录仪产出:

myString: 4.5
myString: 000

如果你不敢动地开始仅仅创造像:

myNumber = [NSNumber numberWithFloat:4.5];
myNumber = [NSNumber numberWithInt:0];

或只是使用标准格式:

myString = [NSString stringWithFormat:@"%.1f", [myNumber floatValue]];
myString = [NSString stringWithFormat:@"%03d", [myNumber intValue]];

或如果你不需要<代码>。 NSNumber 代表仅使用标准格式:

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

You could try something like:

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

印本之后,将印刷至少3位数,并用0个空位印制。

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);

既然经常有人问这个问题, Apple果docs,那是人们要找的答案。 以下链接有两个解决办法。 1个使用<代码> WithFormat: and the other using NSNumberFormatter.

https://stackoverflow.com/a/11131497/1058199





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签