使用 stringWithFormat 在 iPhone 的标签区域显示数值
我是 Objective C 和 iPhone SDK 的新手,我正在尝试将以下内容作为在标签区域中显示数字结果的简单示例:
label.text = [NSString stringWithFormat: @"%d", 55];
上面的代码在标签区域中显示数字“55”。但是,以下代码会导致显示“0”(在头文件中将 CalculationResult 声明为双变量类型):
calculationResult = 55;
label.text = [NSString stringWithFormat: @"%d", calculationResult];
非常感谢任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
%f
作为浮点数或双精度数。如果您不想打印任何小数位,则应使用%.0f
。You should use
%f
for a float or double. If you don't want to print any decimal places, you should use%.0f
.如果要显示双精度数,请使用
%f
表示法,而不是%d
-%d
代表 d十进制数字,而%f
用于f浮点类型(包括双精度)。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).