This content has been marked as final. Show 9 replies
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));
simply use java.text.DecimalFormat
zetadisp = (int)(Math.round(zeta*100));replace with these stmt
I then print the result to screen using....
bufferg.drawString("Damping ratio: " + (0.01*zetadisp), 650, 55);
DecimalFormat d = new DecimalFormat("0.00"); bufferg.drawString("Damping ratio: " + (d.format(zeta)), 650, 55);
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.
or just use string String.format... if performance isn't a consideration.
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"
for information, my header reads:
//Caravan loading program
//Written by CPGL
//Applet will calculate nose weight, mass and inertia of a caravan and use this to
//determine damping ratio (crudely speaking)
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!
...Try finding that answer yourself by using your favorite search engine to see in what package DecimalFormat resides (and thus, need to import).
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 forumYou're welcome.
by the way and I'm totally sold!
Prometheuz - I take your point!
OK all sorted.
Prometheuz - I take your point!No problem, and good to hear you got things running!
OK all sorted.