找到"字符串长度QUOT;一个int

找到"字符串长度QUOT;一个int

问题描述:

基本上我想要返回的位数在INT - >值是这样的:

basically I want to return the number of digits in the int -> values like this:

(int)1 => 1
(int)123 => 3
(int)12345678 => 8

我一无所知C,所以请多多包涵。我知道目标C,但我用整型和浮点型,而不是NSNumbers。我知道我可以在整数转换成目标C对象,但这似乎faffy,如果我可以用C做,我就知道它的未来。

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.

感谢

使用

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)));

这为负号加1,如果是负的。