Displaying numeric value in label area of iPhone using stringWithFormat

user183804 picture user183804 · Oct 5, 2009 · Viewed 9.5k times · Source

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.

Answer

Benno picture Benno · Oct 5, 2009

You should use %f for a float or double. If you don't want to print any decimal places, you should use %.0f.