I´m trying to set the number of digits of a float with a segmented control.
So I've created an segmented control with "0", "1", "2" and "3".
I want to set the digits after the comma with a variable (self._segmentedControl.selectedSegmentIndex
).
I know that I can decide how many digits after a comma should be like this:
sliderValue.text = [NSString stringWithFormat:@"%.3f",slider.value];
Can someone please help me?
The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.
Rounding Numbers in SwiftBy using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.
You can even do it in one step:
sliderValue.text = [NSString stringWithFormat:@"%.*f", numberOfDigits, slider.value];
You could do this in two steps.
NSString *format = [NSString stringWithFormat:@"%%.%df", numberOfDigits);
sliderValue.text = [NSString stringWithFormat:format, slider.value];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With