Displaying numeric value in label area of iPhone using stringWithFormat
-
19-09-2019 - |
Question
I am new to Objective C and the iPhone SDK, and am trying to figure out the following as a simple example of displaying a numeric result in a label area:
label.text = [NSString stringWithFormat: @"%d", 55];
This code above displays the number "55" in the label area. However, the following code results in the display of "0" (with calculationResult declared as a double variable type in the header file):
calculationResult = 55;
label.text = [NSString stringWithFormat: @"%d", calculationResult];
Any help is much appreciated.
Solution
You should use %f
for a float or double. If you don't want to print any decimal places, you should use %.0f
.
OTHER TIPS
If you want to display a double, use the %f
notation, not %d
- %d
is for decimal numbers, while %f
is for floating point types (including doubles).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow