C语言 找到一个 int 的“字符串长度”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4143000/
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
find the "string length" of an int
提问by Thomas Clayson
basically I want to return the number of digits in the int -> values like this:
基本上我想返回 int -> 值中的位数,如下所示:
(int)1 => 1
(int)123 => 3
(int)12345678 => 8
I know nothing about C, so please bear with me. I know objective c, but I use ints and floats instead of NSNumbers. I realise I could convert the ints into objective c objects, but this seems faffy, and if I can do it with C I'll know it for the future.
我对 C 一无所知,所以请耐心等待。我知道目标 c,但我使用整数和浮点数而不是 NSNumbers。我意识到我可以将整数转换为客观的 c 对象,但这似乎很愚蠢,如果我可以用 C 来做到这一点,我将来就会知道。
Thanks
谢谢
回答by MartinStettner
use
用
int d = (value == 0 ? 1 : (int)(log10(value)+1));
Note that this doesnt work for negative numbers, you'll have to use
请注意,这不适用于负数,您必须使用
int d = (value == 0 ? 1 : ((int)(log10(fabs(value))+1) + (value < 0 ? 1 : 0)));
which adds 1 for the minus sign, if valueis negative.
如果value为负,则为减号加 1 。
回答by Curd
Probably much faster than using log or int-to-string conversion and without using any library functions is this:
可能比使用 log 或 int-to-string 转换并且不使用任何库函数要快得多的是:
int nDigits(int i)
{
if (i < 0) i = -i;
if (i < 10) return 1;
if (i < 100) return 2;
if (i < 1000) return 3;
if (i < 10000) return 4;
if (i < 100000) return 5;
if (i < 1000000) return 6;
if (i < 10000000) return 7;
if (i < 100000000) return 8;
if (i < 1000000000) return 9;
return 10;
}
EDITafter Jeff Yates concerns:
在杰夫耶茨担心之后编辑:
For those who worry about int sizes different from 32-bits (similar to pmg's solution but still faster because multiplication is faster than division :-)
对于那些担心 int 大小与 32 位不同的人(类似于 pmg 的解决方案,但仍然更快,因为乘法比除法快:-)
#include <limits.h>
#define PO10_LIMIT (INT_MAX/10)
int nDigits(int i)
{
int n,po10;
if (i < 0) i = -i;
n=1;
po10=10;
while(i>=po10)
{
n++;
if (po10 > PO10_LIMIT) break;
po10*=10;
}
return n;
}
回答by DVK
Use logarithms base 10:
使用以 10 为底的对数:
int length = (int)floor(log10((float)number)) + 1; // works for >0
回答by pmg
Here's another option
这是另一种选择
int nDigits(unsigned i) {
int n = 1;
while (i > 9) {
n++;
i /= 10;
}
return n;
}
This is faster than using log10, but slower than Curd's option with the cascading tests. However it doesn't assume ints are 32 bits :-)
这比 using 快log10,但比使用级联测试的 Curd 选项慢。但是它不假设ints 是 32 位 :-)
回答by aib
A more general solution, especially if you want to know the length for purposes of printing with printf()variants is:
一个更通用的解决方案,特别是如果您想知道用于打印printf()变体的长度是:
snprintf(NULL, 0, "%d", myint);
The return value should tell you the length of the string that would be printed.
返回值应该告诉您将打印的字符串的长度。
回答by ManuelAtWork
If your integer value (e.g. 12345678u) is a compile-time constant, you can let the compiler determine the length for you:
如果您的整数值(例如12345678u)是编译时常量,您可以让编译器为您确定长度:
template<typename T>
constexpr unsigned int_decimal_digits(T value)
{
return ( value / 10
? int_decimal_digits<T>(value/10) + 1
: 1 );
}
Usage:
用法:
unsigned n = int_decimal_digits(1234);
// n = 4
#include <limits.h>
unsigned m = int_decimal_digits(ULLONG_MAX);
// m = maximum length of a "long long unsigned" on your platform
This way, the compiler will compute the number of decimal places automatically, and fill in the value as a constant. It should be the fastest possible solution, because there is no run-time computation involved and integer constants are usually put into the instruction opcodes. (This means that they travel by instruction pipeline, not by data memory/cache.) However, this requires a compiler that supports C++11.
这样,编译器会自动计算小数位数,并将值填入常量。它应该是最快的解决方案,因为不涉及运行时计算并且整数常量通常被放入指令操作码中。(这意味着它们通过指令管道传输,而不是通过数据内存/缓存。)但是,这需要支持 C++11 的编译器。

