如何确定C中整数的位数? [英] How do I determine the number of digits of an integer in C?

查看:18
本文介绍了如何确定C中整数的位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

n = 3432, result 4

n = 45, result 2

n = 33215, result 5

n = -357, result 3

我想我可以把它变成一个字符串然后得到字符串的长度,但这看起来很复杂而且很麻烦.

I guess I could just turn it into a string then get the length of the string but that seems convoluted and hack-y.

推荐答案

floor (log10 (abs (x))) + 1

http://en.wikipedia.org/wiki/Logarithm

这篇关于如何确定C中整数的位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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