有没有比这个方法更简洁的获取一个整数位数的方式?
int numDigits = String.valueOf(1000).length();
有没有比这个方法更简洁的获取一个整数位数的方式?
int numDigits = String.valueOf(1000).length();
这个递归方法怎么样?
private static int length = 0;
public static int length(int n) {
length++;
if((n / 10) < 10) {
length++;
} else {
length(n / 10);
}
return length;
}
public int numLength(int n) {
for (int length = 1; n % Math.pow(10, length) != n; length++) {}
return length;
}
大多数人之所以这样做,主要是因为他们想要“呈现”它,这通常意味着它最终需要被显式或隐式地“toString-ed”(或以其他方式转换);在它可以被呈现(例如打印)之前。
如果是这种情况,那么只需尝试使必要的“toString”显式,并计算位数即可。
以下是JDK开发人员的解决方案。这是JDK 17(类Long
):
/**
* Returns the string representation size for a given long value.
*
* @param x long value
* @return string size
*
* @implNote There are other ways to compute this: e.g. binary search,
* but values are biased heavily towards zero, and therefore linear search
* wins. The iteration results are also routinely inlined in the generated
* code after loop unrolling.
*/
static int stringSize(long x) {
int d = 1;
if (x >= 0) {
d = 0;
x = -x;
}
long p = -10;
for (int i = 1; i < 19; i++) {
if (x > p)
return i + d;
p = 10 * p;
}
return 19 + d;
}
请注意,该方法会考虑到负号的情况,如果有必要的话。
不幸的是,该方法没有被公开。
就性能而言,从评论中可以看出,与其他替代方案相比,JDK开发人员至少对此进行了一些思考。我猜测,一个偏向较小数字的分治方法可能会稍微更好一些,因为CPU可以比整数乘法更快地进行整数比较。但是差异可能非常小,无法测量。
无论如何,我希望这个方法在JDK中被公开,这样人们就不会开始自己编写类似的方法了。
计算一个 int
变量中数字个数的有效方法之一是定义一个名为 digitsCounter 的方法,并需要一定数量的条件语句。
这种方法很简单,我们将检查每个范围内可以包含一个 n
位数字的情况:
0 : 9 是 单
位数字
10 : 99 是 双
位数字
100 : 999 是 三
位数字,以此类推...
static int digitsCounter(int N)
{ // N = Math.abs(N); // if `N` is -ve
if (0 <= N && N <= 9) return 1;
if (10 <= N && N <= 99) return 2;
if (100 <= N && N <= 999) return 3;
if (1000 <= N && N <= 9999) return 4;
if (10000 <= N && N <= 99999) return 5;
if (100000 <= N && N <= 999999) return 6;
if (1000000 <= N && N <= 9999999) return 7;
if (10000000 <= N && N <= 99999999) return 8;
if (100000000 <= N && N <= 999999999) return 9;
return 10;
}
static int digitsCounter(int N)
{
N = N < 0 ? -N : N;
if (N <= 9) return 1;
if (N <= 99) return 2;
if (N <= 999) return 3;
if (N <= 9999) return 4;
if (N <= 99999) return 5;
if (N <= 999999) return 6;
if (N <= 9999999) return 7;
if (N <= 99999999) return 8;
if (N <= 999999999) return 9;
return 10; // Max possible digits in an 'int'
}
简单的解决方案:
public class long_length {
long x,l=1,n;
for (n=10;n<x;n*=10){
if (x/n!=0){
l++;
}
}
System.out.print(l);
}
在查看了 Integer.java
的源代码后,我编写了这个函数。
private static int stringSize(int x) {
final int[] sizeTable = {9, 99, 999, 9_999, 99_999, 999_999, 9_999_999,
99_999_999, 999_999_999, Integer.MAX_VALUE};
for (int i = 0; ; ++i) {
if (x <= sizeTable[i]) {
return i + 1;
}
}
}
public static int getNumberOfDigits(int number) {
int numberOfDigits = 0;
while(number != 0) {
number /= 10;
numberOfDigits++;
}
return numberOfDigits;
}
这是我制作的一个非常简单的方法,适用于任何数字:
public static int numberLength(int userNumber) {
int numberCounter = 10;
boolean condition = true;
int digitLength = 1;
while (condition) {
int numberRatio = userNumber / numberCounter;
if (numberRatio < 1) {
condition = false;
} else {
digitLength++;
numberCounter *= 10;
}
}
return digitLength;
}
int number = 103342;
,则会得到6,因为这相当于向后移动.000001个空间。另外,有没有人有一个更好的numberCounter
变量名称?我想不出更好的名称。public static int repeatingLength(double decimalNumber) {
int numberCounter = 1;
boolean condition = true;
int digitLength = 1;
while (condition) {
double numberRatio = decimalNumber * numberCounter;
if ((numberRatio - Math.round(numberRatio)) < 0.0000001) {
condition = false;
} else {
digitLength++;
numberCounter *= 10;
}
}
return digitLength - 1;
}