如何设置显示数字的填充字符(目标 C)
我正在尝试格式化一个字符串,以便为我提供 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与 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.)