I am trying to figure out why I am getting this loss of precision error in my Java program.
This is the error:
error: possible loss of precision
int digit = num/Math.pow(10,i-1)%10;
^
required: int
found: double
1 error
This is the code clip inside a for loop
for(int i = 1; i<=String.valueOf(num).length(); i++)
{
int digit = num/Math.pow(10,i-1)%10;
//etc
}
I want to keep digit as an integer variable, and I tried to declare digit outside the loop, and I also tried casting it into an integer which didn't work. I did try doing it as:
digit += num/Math.pow(10,i-1)%10; //with digit declare outside of loop
which worked magically, but I don't want to increment it, instead I want it to contain the digit which I can use for later. Is there something I'm missing here? Please explain.