This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Strange Result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange Result" Watch "Strange Result" New topic
Author

Strange Result

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249



Instead of showing 0.90 it's showing 0.8999999999999999. Could anyone give the reason.


The Best way to predict your future is to create it - Every great individual common man
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

See point 23 of the Beginner's FAQ for an explanation.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 95
It's because of the binary representation of a double, which holds up to 15 digits of precision.

You can get the result you want in at least two ways:

1. Convert the literals from a double to a float (4 bytes instead of 8, and roughly 7 digits of precision), as follows:



2. Format the result to print the number of digits you want, which will round appropriately, as follows:

import java.text.DecimalFormat;



For further information and examples, Google "Number Format Java"
[ April 12, 2006: Message edited by: Tom Fulton ]
 
Consider Paul's rocket mass heater.
 
subject: Strange Result