固定的整数打印位数(例如:0001到0100) [英] Fixed number of digits printing of integers (ex: 0001 to 0100)

查看:127
本文介绍了固定的整数打印位数(例如:0001到0100)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不出一个简单的方法来打印具有固定数量

位数的整数,以获得类似0001的1和0100的100

自动只需在变量中指定所需的

位数。


我考虑使用例如%4d打印到变量然后

将''''替换为'''',但我不知道我是否可以打印到阵列,并且

然后我不认为我可以在%和d之间放一个变量来指定我想要的

位数。


我想也许会有一些已经存在且容易方式

这样做我不知道,否则我想知道人们通常如何处理这个问题,谢谢

解决方案

Michel Rouzicaécrit:

我想不出一个用修复打印整数的简单方法的数量/>数字,以便通过在变量中指定所需的
数字来自动获得类似0001 for 1和0100 for 100
的东西。

我考虑过打印使用例如%4d的变量然后
将''''替换为''0'',但我不知道我是否可以打印到阵列,然后我不知道我想我可以在%和d之间加一个变量来指定我想要的数字位数。

我想也许会有一些已存在且简单的方法
这样做我不知道,否则我想知道人们通常如何处理这个问题,谢谢




# include< stdio.h>

int main(void)

{

for(int i = 4; i< 10; i ++)

printf("%05d \ n",i * 100);

int width = 6;

printf("%0 * d \ n",width,746);

}


输出

00400

00500

00600

00700

00800

00900

000746




" Michel Rouzic" <弥******** @ yahoo.fr>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

我不能想到一个简单的方法来打印具有固定数量的
数字的整数,以便通过指定自动获得类似0001 for 1和0100 for 100
的内容变量所需数字的数量。

我考虑使用例如%4d打印到变量然后
将''''替换为''0'',但是我不知道我是否可以打印到数组,然后我不认为我可以在%和d之间加一个变量来指定我想要的数字位数。 br />
我想也许会有一些已经存在且简单的方法来做我不知道的事情,否则我想知道人们是怎样的
通常处理这个问题,谢谢




#include< stdio.h>


int main()

{

int i = 0;

i $ 1; i< 101; ++ i)

printf("%04d \ n",i);

返回0;

}


你的教科书在哪里?


-Mike




Michel Rouzic写道:

我想不出一个简单的方法来打印整数修复
数字的数量,以便通过在变量中指定所需的
数字来自动获得类似0001 for 1和0100 for 100
的内容。
我考虑使用例如%4d打印到变量然后
将''''替换为''0'',但我不知道我是否可以打印到数组,并且
然后我不认为我可以在%和d之间加一个变量来指定我想要的数字位数。

我想也许会有一些已经存在且容易的这样做我不知道的,否则我想知道人们是怎样的>通常处理这个问题,谢谢




尝试%04d


-David


I can''t think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace '' '' by ''0'', but I don''t know if i can print to an array, and
then I don''t think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don''t know of, otherwise i''d like to know how people
usually deal with that problem, thanks

解决方案

Michel Rouzic a écrit :

I can''t think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace '' '' by ''0'', but I don''t know if i can print to an array, and
then I don''t think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don''t know of, otherwise i''d like to know how people
usually deal with that problem, thanks



#include <stdio.h>
int main(void)
{
for (int i=4; i<10;i++)
printf("%05d\n",i*100);
int width=6;
printf("%0*d\n",width,746);
}

Output
00400
00500
00600
00700
00800
00900
000746



"Michel Rouzic" <Mi********@yahoo.fr> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

I can''t think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace '' '' by ''0'', but I don''t know if i can print to an array, and
then I don''t think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don''t know of, otherwise i''d like to know how people
usually deal with that problem, thanks



#include <stdio.h>

int main()
{
int i = 0;

for(i = 1; i < 101; ++i)
printf("%04d\n", i);

return 0;
}

Where''s your textbook?

-Mike



Michel Rouzic wrote:

I can''t think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace '' '' by ''0'', but I don''t know if i can print to an array, and
then I don''t think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don''t know of, otherwise i''d like to know how people
usually deal with that problem, thanks



Try "%04d"

-David


这篇关于固定的整数打印位数(例如:0001到0100)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆