NSNumberFormatter numberFromString问题

时间:2015-06-29 00:44:37

标签: ios objective-c nsstring nsnumber nsnumberformatter

我需要从UITextfield获取一个字符串并将其格式化为以下内容:

rbenv

问题是当我在文本字段中键入0.999978并且格式化的数字是0.999978(这不是正确的)时它应该是0.999

1 个答案:

答案 0 :(得分:1)

尝试一下:

NSNumber  *aNum = [NSNumber numberWithDouble:[string doubleValue]];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setPositiveFormat:@"##0.###"];
[numberFormatter setRoundingMode:NSNumberFormatterRoundFloor];

DLog(@"formatted string %@",[numberFormatter stringFromNumber:aNum]);