1

在 iOS 中,我有一个距离选择器:

  0.25 miles
  1.00 miles
  3.00 miles

并呈现为:

cell.textLabel.text=[NSString stringWithFormat: @"%.2f miles", [i floatValue]];

我将如何渲染:

0.25
1 
3

谢谢

4

2 回答 2

2

尝试使用NSNumberFormatter. 一个简短的例子:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
double d1 = 0.25;
double d2 = 1.00;
double d3 = 3.00;
NSLog(@"d1: %@\nd2: %@\nd3: %@", [numberFormatter stringFromNumber:[NSNumber numberWithDouble:d1]], [numberFormatter stringFromNumber:[NSNumber numberWithDouble:d2]], [numberFormatter stringFromNumber:[NSNumber numberWithDouble:d3]]);

输出:

2012-05-16 07:50:17.153 testApp[266:207] d1: 0,25
d2: 1
d3: 3

希望能帮助到你

于 2012-05-16T05:52:02.140 回答
2

你的意思是,如果它小于一个,做别的事情?

if ([i floatValue]<1) {
   cell.textLabel.text=[NSString stringWithFormat: @"%.2f miles", [i floatValue]];
}
else {
   cell.textLabel.text=[NSString stringWithFormat: @"%.0f miles", [i floatValue]];
}
于 2012-05-16T05:49:35.673 回答