本文介绍了格式字符串,带前导零的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个整数值转换为一个带有前导零的字符串(如果需要),这样该字符串总共有 3 个字符.例如,5
会变成 005"
,10
会变成 010"
.p>
我试过这段代码:
NSString* strName = [NSString stringWithFormat:@"img_00%d.jpg", i];
这部分有效,但如果 i
的值为 10
,例如,结果是 img_0010.jpg
,而不是 img_010.jpg
.
有没有办法做我想做的事?
解决方案
使用格式字符串"img_%03d.jpg"
获取三位前导零的十进制数.
I want to convert an integer value to a string with leading zeroes (if necessary) such that the string has 3 total characters. For example, 5
would become "005"
, and 10
would become "010"
.
I've tried this code:
NSString* strName = [NSString stringWithFormat:@"img_00%d.jpg", i];
This partially works, but if i
has the value of 10
, for example, the result is img_0010.jpg
, not img_010.jpg
.
Is there a way to do what I want?
解决方案
Use the format string "img_%03d.jpg"
to get decimal numbers with three digits and leading zeros.
这篇关于格式字符串,带前导零的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!