title: Convert from double to long without Round Java
body: <p>I'm trying to convert a random double to a long without rounding or truncating it.
  First I change the double to a string, to know how many decimal places there are and then I change the value to long.
  My problem is that the number of the last decimal place is not always correct and I don't know the reason and how I can change it.
  Here is the code:</p>
  
  <pre><code>            double z=(double) myRandom(1, 20);
      long test;
      String s = Double.toString(z);
      test=(long) (Math.pow(10, s.length()-s.indexOf(".")-1)*z);
      System.out.println("z: "+z);    
      System.out.println("double converted to long: "+test);
  </code></pre>
  
  <p>And that's the output:</p>
  
  <p>d: 19.625014811604743</p>
  
  <p>double converted to long: 19625014811604744</p>
  
  <p>d: 9.143326452202839</p>
  
  <p>double converted to long: 9143326452202838</p>
  
  <p>d: 5.8964228376511105</p>
  
  <p>double converted to long: 58964228376511104</p>
  
  <p>d: 15.045936360299917</p>
  
  <p>double converted to long: 15045936360299918</p>
  
  <p>d: 14.147950026532694</p>
  
  <p>double converted to long: 14147950026532694</p>