使用 stringWithFormat 在 iPhone 的标签区域显示数值

发布于 2024-08-06 10:08:14 字数 364 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2024-08-13 10:08:15

您应该使用 %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.

も星光 2024-08-13 10:08:15

如果要显示双精度数,请使用 %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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文