iphone - 在 Objective-C 字符串格式中添加零

标签 iphone objective-c formatting nsstring numbers

简单问题:我试图在 NSString stringWithFormat 格式化字符串中用特定数量的零填充空格。
例如,我想要:

@"The number is %d", 5   // I want this to output 'the number is 05'
@"the number is %d", 10  // I want this to output 'the number is 10'

我知道如何在 Java 中执行此操作,但我似乎无法在 Objective-C 中找到相同的函数。

任何帮助都会很棒。

最佳答案

如果在 Java 中使用 System.out.printf(),那么它与 Objective-C(以及 C,就此而言)中的格式语法相同:

NSLog(@"The number is %02d", 5);
NSLog(@"The number is %02d", 10);

关于iphone - 在 Objective-C 字符串格式中添加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697510/

相关文章:

iphone - UIAlertView 能否通过委托(delegate)传递字符串和 int

ios - 如何在不重新加载 UITableView 或单个单元格的情况下增加单行高度 [Swift]?

objective-c - (iOS) 为什么 AVAudioPlayer initWithContentsOfURL 总是失败,错误代码=-43

objective-c - MPMoviePlayerViewController 后台处理。从后台返回前台后如何继续播放?

iPhone OpenGL fps 测试

html - 为什么我的第二个 <p> 标签的格式与第一个不同?

java - 有人可以帮我用java中的for循环格式化二维数组显示吗

iphone - 苹果触摸图标的正确像素尺寸是多少?

格式化日期时 NodeJS 小时内的 SQL 查询关闭

iphone - 在 Objective C、XCode 中定义类