File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Somebody understand this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Somebody understand this" Watch "Somebody understand this" New topic

Somebody understand this

David Cuesta

Joined: Nov 07, 2001
Posts: 25
The code:
public class prDouble{
public static void main(String []arg){
double dYLDAux = Double.parseDouble("+0.0466");
double dHSTAux = Double.parseDouble("+4.9842");
double dAux;
dAux = dYLDAux/dHSTAux;
System.out.println(dAux); //0.009349544560812165
System.out.println(dAux); //0.0090
do Somebody know why the last zero appears?
I try with others decimal places and this problem don't occurs.
Thanks for all,
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Hmmm... odd little quirk there, isn't it? I can appreciate your problem but can offer no solution or explanation although you might try using the Double.doubleToLongBits() and Double.doubleToRawLongBits() and see if you can find variances in what the different powers of ten give you.
[ March 05, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
I don't really have an answer for you but I think that it has something to do with the fact that Math.round(a) = (long)Math.floor(a + 0.5d)

Hope This Helps
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
This has nothing to do with Math.round(). This prints out even if you write
Actually, it's just a formatting issue. println() is implementing a specific formating algorithm, and according to the rules of that particular algorithm, that's how that particular floating point number is being displayed. (Actually println() is using a package-private class FloatingDecimal to do the formatting)
Keep in mind there is a difference between a number's value and the way that number is formatted for display.
For example, say I just have a regular old floating point value like .0090
There's no particular reason why I can't just as well choose to represent that value as:
There are infact, infinate ways to represent this quantity. The println()method, when asked to display a floating point double value must pick ONE of these infinate ways, and the method the author of the formatting routine chose results in ".0090".
If you don't like this, you are free to create your own format, using NumberFormat, or more specifically, DecimalFormat. Then you can *always* choose to fomat a value like .0090 for display as ".009" if you like.
[ March 05, 2002: Message edited by: Rob Ross ]

SCJP 1.4
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Actually println() is using a package-private class FloatingDecimal to do the formatting
Rob is correct and has obviously been gophering around the source code. The FloatingDecimal class actually uses Double.doubleToLongBits() to figure out how to display the value, so I was on the right track there, too (just don't ask me to explain that algorithm--we'll leave that up to Rob )
I agree. Here's the link:
subject: Somebody understand this
It's not a secret anymore!