9 Replies Latest reply on Sep 3, 2007 12:01 PM by 800282

# Strange behaviour of int and double results

Hi,

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.

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
Chris
• ###### 1. Re: Strange behaviour of int and double results
Instead of rounding, use the DecimalFormat class:
``````DecimalFormat df = new DecimalFormat("0.00");
double value = 0.470000000000000003;
System.out.println(value+" formatted = "+df.format(value));``````
• ###### 2. Re: Strange behaviour of int and double results
hi,

simply use java.text.DecimalFormat
``http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html``
• ###### 3. Re: Strange behaviour of int and double results
I then print the result to screen using....
bufferg.drawString("Damping ratio: " + (0.01*zetadisp), 650, 55);
replace with these stmt
``````DecimalFormat d = new DecimalFormat("0.00");
bufferg.drawString("Damping ratio: " + (d.format(zeta)), 650, 55);``````
• ###### 4. Re: Strange behaviour of int and double results
You really need to bone up on floating point number representations... Java's double is like (but is not the same as) the 64 bit floating point representation specified by IEE754... you can google for that, or for the "java language specification floating point".

But yeah, I reckon javax.swing.text.NumberFormatter (or java.text.NumberFormat) will solve your immediate problem.
• ###### 5. Re: Strange behaviour of int and double results
or just use string String.format... if performance isn't a consideration.
• ###### 6. Re: Strange behaviour of int and double results
Thanks all for your help. I'm looking into the code you sent me but at the moment I get a compile error:

"DecimalFormat cannot be resolved into a type"

//
//Written by CPGL
//August/September 2007
//

//
//Applet will calculate nose weight, mass and inertia of a caravan and use this to
//determine damping ratio (crudely speaking)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;

public class caravansnake6 extends Applet
implements MouseListener, MouseMotionListener, ActionListener, Runnable
{

Do I need to import some other class to be able to use the DecimalFormat code?

thanks again - this is my first real use of a forum by the way and I'm totally sold!

cheers
Chris
• ###### 7. Re: Strange behaviour of int and double results
...
Do I need to import some other class to be able to
use the DecimalFormat code?
Try finding that answer yourself by using your favorite search engine to see in what package DecimalFormat resides (and thus, need to import).

thanks again - this is my first real use of a forum
by the way and I'm totally sold!

cheers
Chris
You're welcome.
• ###### 8. Re: Strange behaviour of int and double results
Prometheuz - I take your point!

OK all sorted.

Many thanks.
• ###### 9. Re: Strange behaviour of int and double results
Prometheuz - I take your point!

OK all sorted.

Many thanks.
No problem, and good to hear you got things running!