Reputation: 2450
I have a little coding problem at the moment. I have a picker with 7 different options, currently for testing purposes I have a label that will display the chosen value of the picker.
The problem that I am having is that my label is only displaying whole numbers, not displaying the entire decimal number, can someone please help me so that I can obtain the full value of the chosen item in the picker?
Here is my code :
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [tempList count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [tempList objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
CGFloat chosenValue;
switch (row) {
case 0:
chosenValue = 0.0000373;
break;
case 1:
chosenValue = 0.0000273;
break;
case 2:
chosenValue = 0.0000233;
break;
case 3:
chosenValue = 0.0000204;
break;
case 4:
chosenValue = 0.0000179;
break;
case 5:
chosenValue = 0.0000169;
break;
case 6:
chosenValue = 0.0000142;
break;
default:
chosenValue = 0;
break;
}
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
self.testLabel.text = [formatter stringFromNumber:@(chosenValue)];
}
Upvotes: 0
Views: 265
Reputation: 91
Try this :
self.testLabel.text = [NSString stringWithFormat:@"%.10f",chosenValue];
instead of:
self.testLabel.text = [formatter stringFromNumber:@(chosenValue)];
Upvotes: 0
Reputation: 381
Why did you not use NSString stringWithFormat method?
self.textLabel.text = [NSString stringWithFormat:@"%.10f", chosenValue];
Upvotes: 0
Reputation: 318774
You need to specify the precision:
self.textLabel.text = [NSString stringWithFormat:@"%.7f", chosenValue];
The %f
format specifier defaults to 5 decimal places.
And you probably need to change chosenValue
to double
to get enough significant digits.
Better yet, since you are using an NSNumberFormatter
, set the number of decimals:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.minimumFractionDigits = 7;
Upvotes: 2