好的,所以如果我有一个 double 开头,我该如何限制它或将数字截断为 1dp,因为我知道它们不需要被查看
double a = 1.6;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:a]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setAlwaysShowsDecimalSeparator:YES];
NSLog(@"a raw: %f", a); //outputs 1.600000
myLabel.text = [NSString stringWithFormat:@"%@",numberAsString];
NSLog(@"Formatted a: %@", numberAsString); //outputs 2
[numberFormatter release];
所以问题是它对值进行四舍五入并且没有留下小数位,我试图让它为 myLabel 输出 1.6。