如何设置显示数字的填充字符(目标 C)

发布于 2024-07-26 19:19:56 字数 228 浏览 3 评论 0原文

我正在尝试格式化一个字符串,以便为我提供 2 个字符的数字,无论其值是多少。 现在,

[NSString stringWithFormat:@"%2d:%2d:%2d",h,m,s)];

对于值 1、2、3,输出是

 1: 2: 3

How do I Change the space to 0's ?

I'm trying to format a string to give me 2 characters for a number, no matter what its value. Right now, I have

[NSString stringWithFormat:@"%2d:%2d:%2d",h,m,s)];

and for the values 1, 2, 3, the output is

 1: 2: 3

How do I change the spaces to 0's ?

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

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

发布评论

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

评论(1

染柒℉ 2024-08-02 19:19:56

这与 C 的 printf 相同(参见 man 3 printf):

[NSString stringWithFormat:@"%02d:%02d:%02d",h,m,s];

(顺便说一句,如果您尝试格式化日期或时间,我建议您查看 NSDateFormatter。)

This is done the same as C's printf (see man 3 printf):

[NSString stringWithFormat:@"%02d:%02d:%02d",h,m,s];

(By the way, if you're trying to format dates or times, I'd suggest looking at NSDateFormatter.)

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