aspose file tools*
The moose likes Beginning Java and the fly likes help with FLOATS.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with FLOATS.." Watch "help with FLOATS.." New topic
Author

help with FLOATS..

Maky
Ranch Hand

Joined: Oct 10, 2001
Posts: 53
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
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
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

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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with FLOATS..
 
Similar Threads
Converting An Application to An Applet (Java) and Adding Fonts and Images to Applet
C++ class to Java
Stateless.ear failed to start EJB error in WAS 6.1 Server
Can anyone explain what is == and equal( ) ?
operators &assigment,