如何获取int变量中的位数? [英] How to get number of digits in int variable?

查看:96
本文介绍了如何获取int变量中的位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试编写一个printf语句,用于设置字段宽度,即

打印一个数字。我正在使用它:


printf("%*",fieldwidth,num_to_print);


然而,我可以'弄清楚如何获得

数字中的位数(我将分配给上面的''fieldwidth''变量)。


我知道我不必用printf明确设置字段宽度,但

我非常想知道如何发现它。


任何人都可以建议我如何获得数字

变量的位数?


谢谢......

解决方案

gu*****@gmail.com

你好,

我正在尝试编写一个printf语句,用于在打印数字时设置字段宽度。我正在使用它:

printf("%*",fieldwidth,num_to_print);


哪些不会有用。

ITYM"%* d"或者任何格式说明符适用于

num_to_print。

然而,我无法弄清楚如何获得
数字中的位数(其中然后我将分配给上面的''fieldwidth''变量)。


如果num_to_print是int类型,你可以使用

int tmp = num_print;


for(fieldwidth = 0; tmp!= 0; tmp / = 10)

fieldwidth + = 1;

if(num_print< 0)

fieldwidth + = 1;

(未经测试)

否则你想要

fieldwidth =(int)ceil(log(fabs(num_print))/ log (10))+(num_print< 0);

对于这些信息来说是相当多的工作。

或者,我只使用1 +(CHAR_BIT * sizeof num_print)/ 3

作为一个过高估计来获得一个总是足够广泛的领域。

我知道我不必用printf明确设置字段宽度,但是
我非常想知道如何发现它。

任何人都可以建议我如何得到数字变量的位数?




注意以上是关于整数类型的。

浮点类型

- 告诉你它们有多少(有效)十进制数字ave

- 并且通常不应该打印到它们的全部精度

如果没有必要


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


< blockquote> Michael Mair写道:

gu*****@gmail.com 写道:

您好,

我正在尝试编写一个printf语句,用于在打印数字时设置字段宽度。我正在使用它:

printf("%*",fieldwidth,num_to_print);



这对任何有用的东西都不起作用。 /> ITYM"%* d"或者任何格式说明符适用于
num_to_print。

但是,我无法弄清楚如何获得
数字中的位数(其中然后我将分配给上面的''fieldwidth''变量。)



如果num_to_print是int类型,你可以使用
int tmp = num_print;

for(fieldwidth = 0; tmp!= 0; tmp / = 10)
fieldwidth + = 1;
if(num_print< 0)
fieldwidth + = 1;
(未经测试)
否则你想要
fieldwidth =(int)ceil(log(fabs(num_print))/ log(10))+(num_print< 0);
其中对于这些信息来说是相当多的工作。
或者,我只是使用1 +(CHAR_BIT * sizeof num_print)/ 3
作为高估来获得一个总是足够宽的场。
< blockquote class =post_quotes>
我知道我不必用printf明确设置字段宽度,但
我非常想知道如何发现它。
< b r />有人可以建议我如何获得数字变量的位数吗?



注意上面是关于整数类型的。
浮点数类型
- 告诉你他们有多少(有效)十进制数字
- 通常不应该打印到他们的全部精度
如果没有必要

干杯迈克尔



这不是很慢吗?为什么不将bitshift转换为值为1的int?


gu ***** @ gmail.com 写道:

#您好,



#我正在尝试编写一个设置的printf语句

#字段宽度#打印一个数字。我正在使用它:



#printf("%*",fieldwidth,num_to_print);



#但是,我无法弄清楚如何获得

#数字中的位数(我将分配给上面的''fieldwidth''变量)。


L = entier(log10(| n |))+ 1,| n |> = 1,是小数点左边的位数。

对于-1< n< 1,通常在小数点左边显示L = 1位(0)。

如果n <0,则为习惯上包括一个负面标记,例如'' - ''。


所有实数都有无限重复或非重复的十进制表示,

所以右边的小数点没有具体限制。对于整数,你

可以使用0.对于浮点数,如果你可以得到硬件支持的大概重要数字b / b,你可以使用R = SL作为| n |> = 1右边的位数

。对于| n |< 1,n!= 0,你可以使用entier(-log10(| n |))

零位后跟S有效数字。


-

SM Ryan http:// www.rawbw.com/~wyrmwif/

如果您打算购物,请告诉我们。

谢谢


Hello,

I''m trying to write a printf statement that sets the field width when
printing a number. I''m using this:

printf("%*", fieldwidth, num_to_print);

However, I can''t figure out how to get the number of digits in the
number (which I would then assign to the ''fieldwidth'' variable above).

I know I don''t have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?

Thanks...

解决方案

gu*****@gmail.com wrote:

Hello,

I''m trying to write a printf statement that sets the field width when
printing a number. I''m using this:

printf("%*", fieldwidth, num_to_print);
Which will not do anything useful.
ITYM "%*d" or whatever format specifier is appropriate for
num_to_print.
However, I can''t figure out how to get the number of digits in the
number (which I would then assign to the ''fieldwidth'' variable above).
If num_to_print is of type int, you could use
int tmp = num_print;

for (fieldwidth = 0; tmp != 0; tmp /= 10)
fieldwidth += 1;
if (num_print < 0)
fieldwidth += 1;
(untested)
Otherwise you want
fieldwidth = (int)ceil(log(fabs(num_print))/log(10)) + (num_print < 0);
which is quite a lot of work for this information.
Alternatively, I would just use 1 + (CHAR_BIT*sizeof num_print)/3
as an overestimation to get an always wide enough field.

I know I don''t have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?



Note that the above is about integer types.
Floating point types
- tell you how many (valid) decimal digits they have
- and usually should not printed to their full precision
if not necessary

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael Mair wrote:

gu*****@gmail.com wrote:

Hello,

I''m trying to write a printf statement that sets the field width when
printing a number. I''m using this:

printf("%*", fieldwidth, num_to_print);


Which will not do anything useful.
ITYM "%*d" or whatever format specifier is appropriate for
num_to_print.

However, I can''t figure out how to get the number of digits in the
number (which I would then assign to the ''fieldwidth'' variable above).


If num_to_print is of type int, you could use
int tmp = num_print;

for (fieldwidth = 0; tmp != 0; tmp /= 10)
fieldwidth += 1;
if (num_print < 0)
fieldwidth += 1;
(untested)
Otherwise you want
fieldwidth = (int)ceil(log(fabs(num_print))/log(10)) + (num_print < 0);
which is quite a lot of work for this information.
Alternatively, I would just use 1 + (CHAR_BIT*sizeof num_print)/3
as an overestimation to get an always wide enough field.


I know I don''t have to explicitly set the field width with printf, but
I would like very much to know how to discover it.

Can anyone suggest how I might get the number of digits for a numeric
variable?


Note that the above is about integer types.
Floating point types
- tell you how many (valid) decimal digits they have
- and usually should not printed to their full precision
if not necessary

Cheers
Michael


Wouldn''t that be quite slow? Why not bitshift an int of value 1?


gu*****@gmail.com wrote:
# Hello,
#
# I''m trying to write a printf statement that sets the field width when
# printing a number. I''m using this:
#
# printf("%*", fieldwidth, num_to_print);
#
# However, I can''t figure out how to get the number of digits in the
# number (which I would then assign to the ''fieldwidth'' variable above).

L = entier(log10(|n|))+1, |n|>=1, is the number of digits left of the decimal point.
For -1<n<1, it is customary to present L=1 digit (''0'') left of the decimal point.
If n<0, it is customary to include a negative mark such as ''-''.

All reals have an infinite repeating or nonrepeating decimal representation,
so that right of the decimal point there''s no specific limit. For integers, you
can use 0. For floats, if you can get the approximate number of significant
digits supported by the hardware, S, you can use R = S-L as the number of digits
to the right for |n|>=1. For |n|<1, n!=0, you can use entier(-log10(|n|))
zero digits followed by S significant digits.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If you plan to shoplift, let us know.
Thanks


这篇关于如何获取int变量中的位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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