I have a double
double pi = 3.1415;
I want to convert this to a int array
int[] piArray = {3,1,4,1,5};
I came up with this
double pi = 3.1415;
String piString = Double.toString(pi).replace(".", "");
int[] piArray = new int[piString.length()];
for (int i = 0; i <= piString.length()-1; i++)
piArray[i] = piString.charAt(i) - '0';
It's working but I don't like this solution because I think a lot of conversions between datatypes can lead to errors. Is my code even complete or do I need to check for something else?
And how would you approach this problem?