title: Numbers missing from stack and linkedlist JAVA?
body: <p>I am trying to implement code to convert decimal to binary with a certain precision, for that I use stack and linked list to add the non decimal and decimal parts calculated. I then use Stringbuilder to pop/poll element one by one to ge the final binary number. SOURCE : <a href="http://www.geeksforgeeks.org/convert-decimal-fraction-binary-number/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/convert-decimal-fraction-binary-number/</a></p>
  
  <p>When I push the elements onto stack/list I see they are being pushed(Using o/p stmts for that). For some reason I dont see them when popping out the elements.</p>
  
  <p>Here is my code</p>
  
  <pre><code>import java.util.LinkedList;
  import java.util.Queue;
  import java.util.Stack;
  </code></pre>
  
  <p>public class BinaryToDecimal {</p>
  
  <pre><code>public String toBinary(float n, int p){
      int non_dec = (int) Math.floor(n);
      Stack&lt;Integer&gt; s_non_dec = new Stack&lt;&gt;();
      LinkedList&lt;Integer&gt; q_dec = new LinkedList&lt;&gt;();
      float dec = n - non_dec;
      int quotient = 1;
      while(quotient &gt; 0){
          quotient = non_dec/2;
          int remainder = non_dec%2;
          System.out.println("quotient"+quotient+"non_dec"+non_dec+"remainder"+remainder);
          s_non_dec.push(remainder);
          non_dec = quotient;
      }
  
      while(p&gt;0){
          System.out.println("before dec"+dec);
          dec = dec*2;
          System.out.println("after dec"+dec);
          if(dec &gt;=1){
              System.out.println("add 1");
              q_dec.add(1);
              dec = dec - 1;
          }
          else{
              System.out.println("add 0");
              q_dec.add(0);
          }
          p--;
      }
      StringBuilder sb = new StringBuilder();
      for(int i=0;i&lt;s_non_dec.size();i++){
          System.out.println("pop"+s_non_dec.peek());
          sb.append(s_non_dec.pop());
      }
      sb.append('.');
      for(int i=0;i&lt;q_dec.size();i++){
          System.out.println("poll"+q_dec.peek());
          sb.append(q_dec.poll());
      }
  
      return sb.toString();
  }
  
  public static void main (String args[]){
      BinaryToDecimal btd = new BinaryToDecimal();
      System.out.println(btd.toBinary(2.47f, 5));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My output :</p>
  
  <pre><code>quotient1non_dec2remainder0
  quotient0non_dec1remainder1
  before dec0.47000003
  after dec0.94000006
  add 0
  before dec0.94000006
  after dec1.8800001
  add 1
  before dec0.8800001
  after dec1.7600002
  add 1
  before dec0.7600002
  after dec1.5200005
  add 1
  before dec0.52000046
  after dec1.0400009
  add 1
  pop1
  poll0
  poll1
  poll1
  1.011
  </code></pre>
  
  <p>as seen above, even tough I push 1 and 0 into my stack, my output has only 1 for the non decimal part instead of 1 and 0! The same happens for the decimal part!
  Ive been looking at this code for hours, any help is appreciated!</p>