I have a UILabel that is currently displaying the value on a UISlider as a value between 0 and 1 in increments of .1 (so it would display .1 or .2 or .3 etc up to 1). However I want to display this value as a % out of 100, but I’m not quite familiar with formatting floating point numbers. Here is the code I have right now:
sliderLabel.text = [NSString stringWithFormat:@"%.1f", [[f numberFromString:thresholdFile] floatValue]];
Haha, this can be tricky….
From the
printfdocs:Thus, what you really want is this:
Let’s break this down, shall we?
%f: Prints the first floating point number in the arguments list, which is:Notice the
* 100part there, which converts a number in the range of0 ... 1to a range of0 ... 100. You may wish to callround()on this if you don’t want a decimal percentage.%%: Prints out an actual%character. This is becauseprintfsearches for switches that begin with the%character, and so if we had one verbatim in the string, it would throw a hissy fit. Thus, we escape that with another%(just like we would with a backslash), and it’s fine.