iphone - 如何设置显示数字的填充字符( objective-c )

标签 iphone cocoa printf stringwithformat

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

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

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

 1: 2: 3

如何将空格更改为 0?

最佳答案

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

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

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

关于iphone - 如何设置显示数字的填充字符( objective-c ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1106971/

相关文章:

objective-c - 使用 PDFDocument initWithData 获取 'unrecognized selector sent to instance'?

c - “%d”需要类型为 'int' 的参数,但参数 2 的类型为 'long unsigned int' [-Wformat=]

c - printf 不使用格式说明符的潜在问题

ios - iOS 7 中 Notes 应用程序的字体效果

带工具栏的操作表中的 iphone uidatepicker?

iphone - 解析后的网页背景在iphone中不透明

c - 在 c 中将字符串传递给 printf

iphone - UITabBar 选择指示器图像填充

iphone - 如何在我的 iPhone 应用程序和 Mac/PC 之间共享文件

swift - 使用 Swift 检测 MacBook 盖子的打开/关闭?