Java 返回整数的第一位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2051817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return first digit of an integer
提问by Tony
How in Java do you return the first digit of an integer.?
在 Java 中你如何返回整数的第一个数字。?
i.e.
IE
345
345
Returns an int of 3.
返回整数 3。
回答by Jon Skeet
The easiestway would be to use String.valueOf(Math.abs((long)x)).charAt(0)
- that will give it you as a char
1. To get that as an integer value, you could just subtract '0' (as in Unicode, '0' to '9' are contiguous).
在最简单的方法是使用String.valueOf(Math.abs((long)x)).charAt(0)
-这将给它,你作为一个char
1。要将其作为整数值,您只需减去 '0'(如在 Unicode 中,'0' 到 '9' 是连续的)。
It's somewhat wasteful, of course. An alternative would just be to take the absolute value, then loop round dividing by 10 until the number is in the range 0-9. If this is homework, that's the answer I'd give. However, I'm not going to provide the code for it because I think it mightbe homework. However, if you provide comments and edit your answer to explain how you're doing and what problems you're running into, we may be able to help.
当然,这有点浪费。另一种方法是取绝对值,然后循环除以 10,直到数字在 0-9 范围内。如果这是家庭作业,这就是我会给出的答案。但是,我不打算提供它的代码,因为我认为这可能是功课。但是,如果您提供评论并编辑您的答案以解释您的工作方式以及您遇到的问题,我们可能会提供帮助。
1One sticky point to note is that the absolute value of Integer.MIN_VALUE
can't be represented as an int
- so you may should first convert to a long
, then use Math.abs
, thendo arithmetic. That's why there's a cast there.
1需要注意的一个要点是, 的绝对值Integer.MIN_VALUE
不能表示为int
- 所以您可能应该先转换为 a long
,然后使用Math.abs
,然后进行算术运算。这就是为什么那里有一个演员。
回答by George Johnston
Homework Hint: Convert it to a string and and return the first character.
作业提示:将其转换为字符串并返回第一个字符。
回答by Carl Smotricz
public static int firstDigit(int n) {
while (n < -9 || 9 < n) n /= 10;
return Math.abs(n);
}
Should handle negative numbers pretty well, too. Will return a negative first digit in that case.
也应该很好地处理负数。在这种情况下将返回负的第一个数字。
回答by liwp
Yet another way:
还有一种方式:
public int firstDigit(int x) {
if (x == 0) return 0;
x = Math.abs(x);
return (int) Math.floor(x / Math.pow(10, Math.floor(Math.log10(x))));
}
回答by Matthew Flynn
Updated: log10 solution:
更新:log10 解决方案:
A variation on the log10 solution with no division.:
没有除法的 log10 解决方案的变体。:
public int getFirstDigit(int x) {
double e = Math.log10(Math.abs((long) x));
return Double.valueOf(Math.pow(10.0, e - Math.floor(e))).intValue());
}
What's it doing?
它在做什么?
- cast the int to long (to handle the MIN_VALUE issue)
- get the absolute value
- calculate the log10
- calculate the floor of the log10
- subtract the floor from the log10 (the difference will be the fraction)
- raise ten to the difference, giving you the value of the first digit.
- 将 int 转换为 long(以处理 MIN_VALUE 问题)
- 得到绝对值
- 计算 log10
- 计算 log10 的下限
- 从 log10 中减去地板(差异将是分数)
- 将 10 提高到差值,得到第一个数字的值。
while loop solution:
while循环解决方案:
To handle Integer.MIN_VALUE and keep Math.abs() and the cast to long out of the loop:
要处理 Integer.MIN_VALUE 并保持 Math.abs() 和演员表在循环之外:
public static int getFirstDigit(int i) {
i = Math.abs(i / (Math.abs((long)i) >= 10 ) ? 10 : 1);
while (i >= 10 )
i /= 10;
return i;
}
回答by Chip Uni
int main(void) {
int num = 3421;
while (num*num + 10 - num*(1 + num) <= 0) {
num *= (num - 0.9*num)/num;
}
std::cout << num << std::endl;
}
回答by fastcodejava
Fastest way would be :
最快的方法是:
- Compute log of the abs(x), then get floor. Let's call it n.
- Divide the number with 10^n
- 计算 abs(x) 的对数,然后得到地板。我们称之为n。
- 将数字除以 10^n
回答by Andreas Dolk
The missing recursive solution:
缺少的递归解决方案:
int getFirstInt(int input) {
if (input > 0 ? input < 10 : input > -10) {
return input > 0 ? input : -input;
}
return getFirstInt(input / 10);
}
I wouldn't use the ternary operator in real life but - isn't it kind of beautiful? ;)
我不会在现实生活中使用三元运算符,但是 - 它不是很漂亮吗?;)
回答by matt b
This is Groovy, but it should be easy to convert to Java:
这是 Groovy,但转换为 Java 应该很容易:
int firstNum(int x) {
a = Math.abs(x)
sig = Math.floor(Math.log10(a))
return a / Math.pow(10, sig)
}
Results:
结果:
groovy> println(firstNum(345))
3groovy> println(firstNum(3452))
3groovy> println(firstNum(-112))
1groovy> println(firstNum(9999))
9groovy> println(firstNum(Integer.MAX_VALUE))
2groovy> println(firstNum(Integer.MIN_VALUE + 1))
2
groovy> println(firstNum(345))
3groovy> println(firstNum(3452))
3groovy> println(firstNum(-112))
1groovy> println(firstNum(9999))
9groovy> println(firstNum(Integer.MAX_VALUE))
2groovy> println(firstNum(Integer.MIN_VALUE + 1))
2
回答by stacker
Ignoring negative values leads to:
忽略负值会导致:
(""+345).charAt(0);