Hi folks I have the following code in my program. The aim is to generate a sense of time, like 1.01, 1.02.. 1.60 ,2.01 etc..
This gives me some strange errors like : 1.16 1.17 1.1800001 1.19 1.2 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1.3 1.31 1.3199999 1.33 Any idea why this is happening ?? [This message has been edited by Maky (edited December 21, 2001).]
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
Hi Maky, It is called round-off error. Anytime you are printing float or double values you should format the results if it is important to you. Look at: java.text.DecimalFormat For example: DecimalFormat df = new DecimalFormat( "#.00" ); System.out.prnitln( df.format( y ) ); will make your output look consistent. Regards, Manfred.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Hi Maky, Surely we have had this talk before - but can you PLEASE re-register with a name that fits the JavaRanch's naming policy. Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name. Thanks, Cindy
"JavaRanch, where the deer and the Certified play" - David O'Meara
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Running this results in "OK" being printed. Precise, sure, perfect, nope. Corey [This message has been edited by Corey McGlone (edited December 21, 2001).]