I am trying to set the number of digits of a float with a segmented control.
So, I created a segmented control with "0", "1", "2" and "3". I want to set decimal digits with variable ( self._segmentedControl.selectedSegmentIndex).
self._segmentedControl.selectedSegmentIndex
I know that I can decide how many digits after the decimal point should be like this:
sliderValue.text = [NSString stringWithFormat:@"%.3f",slider.value];
Can anyone help me out?
You can do this in one step:
sliderValue.text = [NSString stringWithFormat:@"%.*f", numberOfDigits, slider.value];
You can do this in two steps.
NSString *format = [NSString stringWithFormat:@"%%.%df", numberOfDigits); sliderValue.text = [NSString stringWithFormat:format, slider.value];
NSString *format = [NSString stringWithFormat:@"%%.%df", numberOfDecimalPlaces]; sliderValue.text = [NSString stringWithFormat:format, slider.value];
. .
Check if it works or not. I tested this only in C ++, but since the documentation for String Format points to IEEE printf , I believe that it should work for Objective-C as well.
sliderValue.text = [NSString stringWithFormat: @"%.*f", places, slider.value];