1 保留2位小数点
//.2代表小数点后面保留2位(2代表保留的数量)
Objective-C
复制代码
1
NSString *string = [NSString stringWithFormat:@"%.2f",M_PI];
输出结果是: 3.14
2 用0补全的方法
Objective-C
复制代码
1
NSInteger count = 5;
2
//02代表:如果count不足2位 用0在最前面补全(2代表总输出的个数)
3
NSString *string = [NSString stringWithFormat:@"%https://www.yuque.com/zhuchongjin/xt3c94/idpz50",count];
输出结果是: 05
3 字符串中有特殊符号%
Objective-C
复制代码
1
NSInteger count = 50;
2
//%是一个特殊符号 如果在NSString中用到%需要如下写法
3
NSString *string = [NSString stringWithFormat:@"%zd%%",count];
4 字符串中有特殊符号
Objective-C
复制代码
1
NSInteger count = 50;
2
//"是一个特殊符号, 如果在NSString中用到"需要用\进行转义
3
NSString *string = [NSString stringWithFormat:@"%zd\"",count];