获取int的位数的方法? [英] Way to get number of digits in an int?

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

问题描述

是否有更简洁的方法来获取int的长度?

Is there a neater way for getting the length of an int as this?

int length = String.valueOf(1000).length();


推荐答案

基于String的解决方案完全没问题,有没什么不整洁的。你必须在数学上认识到,数字没有长度,也没有数字。长度和数字都是特定基数中的数字的物理表示的属性,即字符串。

Your String-based solution is perfectly OK, there is nothing "un-neat" about it. You have to realize that mathematically, numbers don't have a length, nor do they have digits. Length and digits are both properties of a physical representation of a number in a specific base, i.e. a String.

基于对数的解决方案(某些)与基于字符串的内部相同的事情,并且可能更快(无关紧要)更快,因为它只产生长度并忽略数字。但我实际上并不认为它的意图更清晰 - 这是最重要的因素。

A logarithm-based solution does (some of) the same things the String-based one does internally, and probably does so (insignificantly) faster because it only produces the length and ignores the digits. But I wouldn't actually consider it clearer in intent - and that's the most important factor.

这篇关于获取int的位数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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