9 Replies Latest reply: Sep 3, 2007 7:01 AM by 800282 RSS

    Strange behaviour of int and double results


      I am rather new to Java and would like some help with a small applet that I have written.

      In the code I calculate a result and store it in a variable of type double. The value of interest varies between 0 and 1.

      double zeta; etc

      zeta = damping(I, Rn, velocity_mph); //my own function

      I am interested in printing only the first two decimal places on the screen, so I perform the conversion and place the result in a new variable `zetadisp' of type int.

      zetadisp = (int)(Math.round(zeta*100));

      I then print the result to screen using....

      bufferg.drawString("Damping ratio: " + (0.01*zetadisp), 650, 55);

      hoping to get a figure 0.71 or 0.43 or whatever and this works fine about 95% of the time.

      But sometimes.....

      the output is "Damping ratio: 0.470000000000000003" !!!!

      Can anyone shed some light on where the rogue decimal places has come from please?

      many thanks