c++ 数字转字符串如何前导补0,一行代码就搞定
对于数字,int转string时经常需要补0,比如日期0201,2月1日。或序列号0001。int n_zero = 4;string old_string = "2";std::string new_string = std::string(n_zero - old_string.length(), '0') + old_string;注意不要让n_zero小于 old_string长度,否则u
·
对于数字,int 转string时经常需要补0,比如 日期0201,2月1日。或序列号0001。
int n_zero = 4;//总共多少位
string old_string = "2";
std::string new_string = std::string(n_zero - old_string.length(), '0') + old_string;
注意不要让n_zero小于 old_string长度,否则unsigned int 会让你崩溃。
c++ - Add leading zero's to string, without (s)printf - Stack Overflow
更多推荐
所有评论(0)