在Java中提取int的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19063001/
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
Extracting digits of int in Java
提问by Sean
So if I have an inputted integer:
所以如果我有一个输入的整数:
int num_1 = 128
How would I be able to parse through the number and obtain a 1, 2 and 8, and assign them to different variables?
我如何能够解析数字并获得 1、2 和 8,并将它们分配给不同的变量?
Thanks!
谢谢!
采纳答案by Omry Yadan
the inefficient way to do this would be to convert the integer to a string and iterate on the string characters.
执行此操作的低效方法是将整数转换为字符串并迭代字符串字符。
the more efficient way would be something like:
更有效的方法是:
int n = 128;
while (n > 0) {
int d = n / 10;
int k = n - d * 10;
n = d;
System.out.println(k);
}
回答by Thilo
Turn it into a String and go character by character for the digits:
将其转换为字符串并逐个字符地查找数字:
char[] digits = String.valueOf(num_1).toCharArray();
回答by óscar López
Here's one way:
这是一种方法:
String digits = Integer.toString(num_1);
int digit1 = Character.digit(digits.charAt(0), 10);
int digit2 = Character.digit(digits.charAt(1), 10);
int digit3 = Character.digit(digits.charAt(2), 10);
Of course, if the integer has more than three digits, using a loop would be more practical:
当然,如果整数超过三位数,使用循环会更实用:
String sDigits = Integer.toString(num_1);
char[] cDigits = sDigits.toCharArray();
int[] digits = new int[cDigits.length];
for (int i = 0; i < cDigits.length; i++)
digits[i] = Character.digit(cDigits[i], 10);
With the above code in place, it's easy to extract the digits from the array:
有了上面的代码,很容易从数组中提取数字:
int digit1 = digits[0];
int digit2 = digits[1];
int digit3 = digits[2];
回答by upog
try
尝试
while (num_1> 0){
int digit = num_1%10;
num_1 = num_1/10;
System.out.println(digit);
}
回答by david
String str = Integer.toString(num_1);
You can obtain 1,2,8 from this str
你可以从这个 str 中获得 1,2,8
回答by alfasin
The answer that Thilo wrote is good but incomplete, you start by doing:
Thilo 写的答案很好但不完整,你从做开始:
char[] digitsAsChars = String.valueOf(num_1).toCharArray();
and then:
进而:
int[] digits = new int[charNums.length];
for(int i=0; i<charNums.length; i++){
digits[i] = charNums[i]-48;//convert char to int
}
now digits
holds the digits of the number as an int array
现在digits
将数字的位数保存为 int 数组
回答by Ashwin Parmar
int num = 128;
String number = String.valueOf(num);
for(int i = 0; i < number.length(); i++) {
int j = Character.digit(number.charAt(i), 10);
System.out.println("digit: " + j);
}
Output:
输出:
digit: 1
digit: 2
digit: 8
回答by Ashwin Parmar
Collect all the digits in the Array and use futher
收集数组中的所有数字并进一步使用
import java.lang.Integer;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class Test
{
public static void main(String[] args) {
Integer num = 12345;
Integer[] digits = getDigits(num.toString());
System.out.println(Arrays.toString(digits));
}
public static Integer[] getDigits(String number) {
List<Integer> digits = new ArrayList<Integer>();
for(int i = 0; i < number.length(); i++) {
int j = Character.digit(number.charAt(i), 10);
digits.add(j);
}
return digits.toArray(new Integer[]{});
}
}
Output should be
输出应该是
[1, 2, 3, 4, 5]
回答by bruhhhhh
Here is a purely mathematical way of doing so:
这是一种纯粹的数学方法:
// Returns digit at pos
static int digitAt(int input, int pos){
int i =(int) (input%(Math.pow(10, pos)));
int j = (int) (i/Math.pow(10, pos-1));
return Math.abs(j); // abs handles negative input
}
For example if input = 1234
and pos = 2
, then i
is 34
. We divide the 34
by 10
and round off to get a 3
.
例如,如果input = 1234
并且pos = 2
,则i
是34
。我们把34
通过10
并四舍五入获得3
。
Not that pretty, but works!
不是那么漂亮,但有效!
回答by BullyWiiPlaza
This code returns the nibble at the given index. If you want to get all digits, you can call this method with all indices of your number. It does not work on the hexadecimal representation of a number but on the decimal one.
此代码返回给定索引处的半字节。如果您想获取所有数字,您可以使用您号码的所有索引调用此方法。它不适用于数字的十六进制表示,但适用于十进制。
public static int getNibble(int number, int nibbleIndex)
{
int nibble = 0;
while (nibbleIndex >= 0)
{
int division = number / 10;
nibble = number - division * 10;
number = division;
nibbleIndex--;
}
return nibble;
}