NSString stringWithFormat - 尝试将单位数字的标签设置为两位数

发布于 2024-08-29 20:22:33 字数 292 浏览 4 评论 0原文

我试图让标签将单位数字显示为两位数字(即 1 => 01)。我可能使用了错误的方法来设置标签的值,但事实是,在翻阅了苹果的文档并搜索了互联网之后,我什至不确定这一点。

[secondsLabel setValue:[NSString stringWithFormat:@"%2d", seconds]]

我看到其他人使用 stringWithFormat 作为 stringWithFormat:@"%.2f" 所以我想我会尝试类似的东西并将其更改为 "%2d",但没有运气。感谢您提前提供帮助。

I am trying to have a label display single-digit numbers as double-digit numbers (ie. 1 => 01) . I may be using the wrong method to set the value for the label but the thing is I'm not even sure about that after pouring over Apple's documentation and scouring the internet.

[secondsLabel setValue:[NSString stringWithFormat:@"%2d", seconds]]

I saw someone else's use of stringWithFormat as stringWithFormat:@"%.2f" so I figured I would try something similar and change it to "%2d", no luck. Thanks for you help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绮筵 2024-09-05 20:22:33

您非常接近:尝试 "%.2d" (注意“.”)。

-stringWithFormat: 方法中格式字符串的参考是 IEEE printf 规范

You were very close: try "%.2d" (note the ".").

The reference for the format string in the -stringWithFormat: method is the IEEE printf specification

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