注:[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];