本文介绍了将Double转换为Int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个程序上工作,用户输入一个double,然后我把double复制到一个数组中(然后我做了一些其他的东西)。
问题是,我不知道如何拆分数字,并把它放入一个int数组。请帮助?
Im working on a program, where the user inputs a double, and I split the double up and put it into an array(Then I do some other stuff).The problem is, im not sure how to split up the double by digit, and put it into an int array. Please help?
Heres what im looking for:
Heres what im looking for:
double x = 999999.99 //thats the max size of the double
//I dont know how to code this part
int[] splitD = {9,9,9,9,9,9}; //the number
int[] splitDec = {9,9}; //the decimal
推荐答案
您可以将数字转换为 String
然后根据分割字符串。
字符。
You can convert the number to String
then split the String based on .
character.
例如:
public static void main(String[] args) {
double x = 999999.99; // thats the max size of the double
// I dont know how to code this part
int[] splitD = { 9, 9, 9, 9, 9, 9 }; // the number
int[] splitDec = { 9, 9 }; // the decimal
// convert number to String
String input = x + "";
// split the number
String[] split = input.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++) {
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
}
// the decimal part
if (split.length > 1) {
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++) {
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
}
这篇关于将Double转换为Int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!