从 Java 中的数字中删除数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20916333/
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
Remove digits from a number in Java
提问by user3159331
How do I remove the first digit of an integer?
如何删除整数的第一个数字?
My input is an integer (for example i = 123456789).
我的输入是一个整数(例如 i = 123456789)。
I then want to remove the first digit, so that i equals 23456789.
然后我想删除第一个数字,以便 i 等于 23456789。
回答by Christian
Here is one way to do it:
这是一种方法:
- Convert it to
String
- Take the substring without the first "digit"
- Convert it to
int
- 将其转换为
String
- 取没有第一个“数字”的子串
- 将其转换为
int
Code:
代码:
public static void main(String[] args)
{
int x = 123456789;
String x_str = Integer.toString(x);
int new_x = Integer.parseInt(x_str.substring(1));
System.out.println(new_x);
}
Output:
输出:
23456789
Note:This can be done in one line with
注意:这可以在一行中完成
int x = 123456789;
int new_x = Integer.parseInt(Integer.toString(x).substring(1));
Edit:
编辑:
To handle negative-case, check if number is positive or integer:
要处理负情况,请检查 number 是正数还是整数:
int new_x = Integer.parseInt(x > 0 ?
Integer.toString(x).substring(1) : Integer.toString(x).substring(2));
回答by Gene
If you want to avoid the string conversion, you can find the high digit and subtract it.
如果你想避免字符串转换,你可以找到高位并减去它。
public static void main(String[] args) {
int x = 123456789;
System.out.println("x = " + x);
int hi = x, n = 0;
while (hi > 9) {
hi /= 10;
++n;
}
for (int i = 0; i < n; i++) hi *= 10;
x -= hi;
System.out.println("x with high digit removed = " + x);
}
回答by Evgeniy Dorofeev
try this
尝试这个
n = n % (int) Math.pow(10, (int) Math.log10(n));
回答by Bohemian
Here's the one-line, purely numeric solution:
这是单行的纯数字解决方案:
i %= (int) Math.pow(10, (int) Math.log10(i));
回答by Darren Gilroy
Alternate approach:
替代方法:
int stripLeading(int i) {
if(i > 0) {
return i - (int)Math.pow(10, (int)Math.log10(i));
} else if(i > 0) {
return i + (int)Math.pow(10, (int)Math.log(-i+1));
} else {
return 0;
}
}
回答by BRPocock
I think I remember the string-free version of this … although I totally agree with @Christian as how I would do it…
我想我记得这个的无字符串版本......虽然我完全同意@Christian我会怎么做......
NOTE: as @Darren Gilroy pointed out, one must consider negatives and zero spocially, and my function fails to do so.
注意:正如@Darren Gilroy 指出的那样,人们必须特别地考虑负数和零,而我的功能却没有这样做。
Of course %
is a better solution also.
当然%
也是更好的解决方案。
public static void main (String [] argv)
{
final int x = 123456789;
int newX = x;
/* How many digits are there? */
final double originalLog = Math.floor (Math.log10 (x));
/* Let's subtract 10 to that power until the number is smaller */
final int getRidOf = (int)Math.pow (10, originalLog);
while (originalLog == Math.floor (Math.log10 (newX)))
{ newX -= getRidOf; }
System.out.println (newX);
}
Poor profiling attempt:
糟糕的分析尝试:
Looping the above function without the println
for 20,000,000,000 repeats in a for
loop:
在没有println
for 20,000,000,000 重复的情况下循环上述函数for
:
real 0m9.943s
user 0m9.890s
sys 0m0.028s
The same with Christian's far-easier-to-understand and perfectly functionable version, but for only 200,000,000 repeats (because I'm lazy and got tired of waiting):
与 Christian 的更容易理解且功能完善的版本相同,但仅重复 200,000,000 次(因为我很懒并且厌倦了等待):
real 0m18.581s
user 0m17.972s
sys 0m0.574s
So one might argue that constructing the String objects is probably slowing it down by roughly 200×, but that isn't a really finely-tuned profiling set-up.
因此,有人可能会争辩说,构造 String 对象可能会使其速度降低大约 200 倍,但这并不是一种真正经过微调的分析设置。
回答by Hungry Blue Dev
If you want to go for simpler methods and without using String
, then here's my simple take:
如果您想采用更简单的方法而不使用String
,那么这是我的简单做法:
- Count number of digits int the integer.
- Divide the
int
by10^n
.n
is the number of digits. - Obtain absolute value of the result. //In case of (-)ve numbers.
- 计算整数的位数。
- 划分
int
的10^n
。n
是位数。 - 获得结果的绝对值。//如果是 (-)ve 个数字。
For example
例如
int i = 123456789;
int n = getDigitCount(i);
int r = Math.abs(i / (int)Math.pow(10,n)); //r stores result.
And you'd require this method:
你需要这个方法:
int getDigitCount(int num)
{
int c = 0;
while(num > 0){
num/=10;
c++;
}
return c;
}