Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C格式字符串-如何将前导零添加到浮点值和int值?_C_Formatting_Placeholder_Output_Printf - Fatal编程技术网

C格式字符串-如何将前导零添加到浮点值和int值?

C格式字符串-如何将前导零添加到浮点值和int值?,c,formatting,placeholder,output,printf,C,Formatting,Placeholder,Output,Printf,可能重复: 我试图让这个C程序的输出有占位符零,因为这个程序的输出将被用作另一个程序的输入。现在,我正在使用下面的打印行 fprintf(fp1, "06 BR%d%d %3.4f%3.4f%3.4f\n",i,d,X,Y,Z); i = index for the loop d = index for a second loop X = double for a Cartesian system Y = double for a Cartesian system Z = double

可能重复:

我试图让这个C程序的输出有占位符零,因为这个程序的输出将被用作另一个程序的输入。现在,我正在使用下面的打印行

fprintf(fp1, "06 BR%d%d   %3.4f%3.4f%3.4f\n",i,d,X,Y,Z);

i = index for the loop
d = index for a second loop
X = double for a Cartesian system
Y = double for a Cartesian system
Z = double for a Cartesian system
现在,输出如下所示:

06 BR12   1.00001.00001.0000
我希望它如下所示:

06 BR0102   001.0000001.0000001.000

我知道我可以手动添加零(例如,如果i您可以使用
%08.4f
进行零位填充。请注意,第一个数字是整个字段宽度,而不仅仅是小数点前所需的位数。在您的示例中,
%3.4
中的
3
无效。如果您希望最后一个数字只有三个小数点,则需要
%07.3f
f。)或者那个


%d
格式更简单-在您的情况下,只要
%02d
就可以了。

下面的格式将帮助您

float a = 1;
int x = 1, y = 2;
printf("06 BR%02d%02d   %08.4f %08.4f %08.4f\n", x, y, a, a, a);
我的输出是

06 BR0102   001.0000 001.0000 001.0000

解释一下就好了。