找到"字符串长度QUOT;一个int [英] find the "string length" of an int

查看:156
本文介绍了找到"字符串长度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,如果是负的。

这篇关于找到&QUOT;字符串长度QUOT;一个int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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