数字中的位数 [英] number of digits in a number
问题描述
你知道一种方法告诉编译器,如果
有1位数字(2,6,8,...) ,我想要这样的空格:
" 。
但如果我有3位数字,我想要空格
像这样:" 。
我不知道如何获得
a数字(整数或浮点数)的数字位数。
谢谢。
Hi,
Do you know a way to tell to the compiler that if
there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this:
" ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don''t know how to get the numbers of digits in
a number (integer or float).
Thanks.
推荐答案
bejiz写道:
bejiz wrote:
你知道一种方法告诉编译器如果
有1位数字(2,6,8,......),我想要这样的空格:
。
但如果我有3位数字,我想要空格
像这样:" 。
我不知道如何获得
中的数字位数(整数或浮点数)。
Hi,
Do you know a way to tell to the compiler that if
there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this:
" ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don''t know how to get the numbers of digits in
a number (integer or float).
''''中的位数(假设''x''是无符号的):
int ndigits = x 0? int(log10(x))+ 1:1;
关于前面的空格数,请参阅''setw''流操纵器。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复顶部-posted回复,请不要问
Number of digits in ''x'' (assuming ''x'' is unsigned) :
int ndigits = x 0 ? int(log10(x)) + 1 : 1;
As to preceding number of spaces, see ''setw'' stream manipulator.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Victor Bazarov写道:
Victor Bazarov wrote:
bejiz写道:
bejiz wrote:
>
你知道一种方法告诉编译器,如果
有1位数字(2,6,8) ,...),我想要这样的空间:
。
但如果我有3位数字,我想要这样的空格
: 。
我不知道如何获得数字中的数字(整数或浮点数)。
>Hi,
Do you know a way to tell to the compiler that if
there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this:
" ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don''t know how to get the numbers of digits in
a number (integer or float).
''x'中的位数(假设''x''是无符号的):
int ndigits = x 0? int(log10(x))+ 1:1;
关于前面的空格数,请参阅''setw''流操纵器。
V
Number of digits in ''x'' (assuming ''x'' is unsigned) :
int ndigits = x 0 ? int(log10(x)) + 1 : 1;
As to preceding number of spaces, see ''setw'' stream manipulator.
V
也许标准保证不然,但这似乎可能会因数字不准确而受到影响。如果log10(100)
返回1.99999999987会发生什么?
Maybe the standard guarantees otherwise, but this seems potentially
susceptible to numerical inaccuracies. What happens if log10( 100)
returns 1.99999999987?
Mark P写道:
Mark P wrote:
Victor Bazarov写道:
Victor Bazarov wrote:
> bejiz写道:
>bejiz wrote:
>>
你知道一种方法告诉编译器
如果有1位数字(2,6,8,...),我想要像
这样的空格: 。
但如果我有3位数字,我想要这样的空格
: 。
我不知道如何获得数字中的数字(整数或浮点数)。
>>Hi,
Do you know a way to tell to the compiler that
if there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like
this: " ".
But if I have numbers of 3 digits, I want spaces
like this: " ".
I don''t know how to get the numbers of digits in
a number (integer or float).
''x''中的位数(假设''x''是无符号的):
int ndigits = x 0? int(log10(x))+ 1:1;
关于前面的空格数,请参阅''setw''流操纵器。
V
Number of digits in ''x'' (assuming ''x'' is unsigned) :
int ndigits = x 0 ? int(log10(x)) + 1 : 1;
As to preceding number of spaces, see ''setw'' stream manipulator.
V
也许标准保证不然,但这似乎可能是因为数字不准确而容易受到影响。如果log10(100)
返回1.99999999987会怎样?
Maybe the standard guarantees otherwise, but this seems potentially
susceptible to numerical inaccuracies. What happens if log10( 100)
returns 1.99999999987?
对。我应该加0.5:
int ndigits = x 0? int(log10(x + 0.5))+ 1:1;
V
-
请删除资金''通过电子邮件回复时的答案
我没有回复最热门的回复,请不要问
Right. I should have added 0.5:
int ndigits = x 0 ? int(log10(x + 0.5)) + 1 : 1;
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于数字中的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!