atoi和以0开头的十进制数字 [英] atoi and leading 0's for decimal numbers
本文介绍了atoi和以0开头的十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C中使用 atoi
时,我试图将数字的 char
数组转换为 int
.我的电话号码上有 0
的前导,以后打印出来时,这些号码不会保留.
When using atoi
in C I am trying to convert a char
array of numbers to an int
. I have leading 0
's on my number though and they are not preserved when I print the number out later.
char num[] = "00905607";
int number;
number = atoi(num);
printf("%d", number);
此输出将为 905607
,我希望其为 00905607
.
The output from this will be 905607
and I would like it to be 00905607
.
有什么想法吗?
推荐答案
您可以在printf()上进行填充,因此如果您希望每个输出的长度为8个字符,则可以使用
You can do padding on your printf() so if you wanted every output to be 8 characters long you would use
printf("%08d", number);
这篇关于atoi和以0开头的十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文