• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with FLOATS..

 
Maky
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Running this results in "OK" being printed. Precise, sure, perfect, nope.
Corey
[This message has been edited by Corey McGlone (edited December 21, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic