title: Java program error: possible loss of precision
body: <p>I am trying to figure out why I am getting this loss of precision error in my Java program.</p>
  
  <p>This is the error:</p>
  
  <pre><code>error: possible loss of precision
           int digit = num/Math.pow(10,i-1)%10;
                                           ^
    required: int
    found:    double
  1 error
  </code></pre>
  
  <p>This is the code clip inside a for loop</p>
  
  <pre><code>for(int i = 1; i&lt;=String.valueOf(num).length(); i++)
  {
      int digit = num/Math.pow(10,i-1)%10;
      //etc
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>digit += num/Math.pow(10,i-1)%10; //with digit declare outside of loop
  </code></pre>
  
  <p>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.</p>