title: Sum of decimal number in java
body: <p>I have a problem for the managemente of decimal number in java (JDK 1.4).</p>
  
  <p>I have two <em>double</em> numbers <strong>first</strong> and <strong>second</strong> (as output of formatted <em>String</em>). I do a sum between fist and second and I receive a number with more decimal digits!</p>
  
  <pre><code>   final double first=198.4;//value extract by unmodifiable format method
  
     final double second=44701.2;//value extract by unmodifiable format method
  
     final double firstDifference= first+second; //I receive 44899.598 instead of 44899.6
  
     final double calculatedDifference=44900.1; // comparison value for the flow
  
      final double error=firstDifference-calculatedDifference;// I receive -0.50390605 instead 0.5
  
      if(Math.abs(error)&lt;=0.5d)){
           //I must enter in this branch but the error not allows the correct flow!!!
      }
      /***
      * the flow of program is uncorrect and there's a very visible bug in business view
      */
  </code></pre>
  
  <p>I prefer not growing the threshold value (<strong>0.5d</strong>) because I'm not safe with similar situation (when I started coding, the specs was talking about <strong>0.1d</strong> as comparison value).
  If it's the only solution, the value of <strong>0.9d</strong> is safest value for this problem?</p>
  
  <p>How I can resolve this situation? I thinked that this problem derive by the use of double variables, but with the float I have the same problem. </p>
  
  <p>Some idea (having some tested code line, if possible ;))?</p>