C++ - 如何找到一个整数的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22648978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - how to find the length of an integer
提问by user1888527
I'm trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use of classes from the STL, although the program spec does say we can use "common C libraries" (gonna ask my professor if I can use cmath, because I'm assuming log10(num) + 1 is the easiest way, but I was wondering if there was another way).
我试图找到一种方法来找到一个整数的长度(位数),然后将它放在一个整数数组中。该作业还要求在不使用 STL 类的情况下执行此操作,尽管程序规范确实说我们可以使用“通用 C 库”(我会问我的教授是否可以使用 cmath,因为我假设 log10(num) + 1 是最简单的方法,但我想知道是否还有其他方法)。
Ah, and this doesn't have to handle negative numbers. Solely non-negative numbers.
啊,这不必处理负数。只有非负数。
I'm attempting to create a variant "MyInt" class that can handle a wider range of values using a dynamic array. Any tips would be appreciated! Thanks!
我正在尝试创建一个变体“MyInt”类,该类可以使用动态数组处理更广泛的值。任何提示将不胜感激!谢谢!
回答by Kerrek SB
The number of digits of an integer n
in any base is trivially obtained by dividing until you're done:
n
任何基数中整数的位数可以通过除法轻松获得,直到完成:
unsigned int number_of_digits = 0;
do {
++number_of_digits;
n /= base;
} while (n);
回答by Riot
Not necessarily the most efficient, but one of the shortest and most readable using C++:
不一定是最有效的,但使用 C++ 时最短和最易读的方法之一:
std::to_string(num).length()
回答by Paul R
回答by droid fiji
There is a much better way to do it
有一个更好的方法来做到这一点
#include<cmath>
...
int size = trunc(log10(num)) + 1
....
works for int and decimal
适用于 int 和 decimal
回答by LihO
"I mean the number of digits in an integer, i.e. "123" has a length of 3"
“我的意思是整数中的位数,即“123”的长度为 3”
int i = 123;
// the "length" of 0 is 1:
int len = 1;
// and for numbers greater than 0:
if (i > 0) {
// we count how many times it can be divided by 10:
// (how many times we can cut off the last digit until we end up with 0)
for (len = 0; i > 0; len++) {
i = i / 10;
}
}
// and that's our "length":
std::cout << len;
outputs 3
产出 3
回答by lvella
Closed formula for the size of the int
:
大小的闭合公式int
:
ceil(8*sizeof(int) * log10(2))
EDIT:
编辑:
For the number of decimal digits of some value:
对于某个值的十进制位数:
ceil(log10(var+1.0))
This works for numbers > 0
. Zero must be checked separately. Negatives must be inverted and added 1 to count for the minus symbol.
这适用于数字> 0
。零必须单独检查。负数必须反转并加 1 以计算减号。
回答by Naheel
int intLength(int i) {
int l=0;
for(;i;i/=10) l++;
return l==0 ? 1 : l;
}
Here's a tiny efficient one
这是一个很小的高效的
回答by John3136
Being a computer nerd and not a maths nerd I'd do:
作为一个计算机书呆子而不是一个数学书呆子,我会这样做:
char buffer[64];
int len = sprintf(buffer, "%d", theNum);
回答by Sir Alucard
How about (works also for 0 and negatives):
怎么样(也适用于 0 和负数):
int digits( int x ) {
return ( (bool) x * (int) log10( abs( x ) ) + 1 );
}
回答by suraz negi
Best way is to find using log, it works always
最好的方法是使用日志查找,它始终有效
int len = ceil(log10(num))+1;