Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regarding comparing two double values.

807588Jun 9 2009 — edited Jun 18 2009
Number Quantity_Remaining = Boundary.getQuantity_remaining ( Value can be anything 1,,2,20,(0.66),(3.54),(55.66),100);
if( Quantity_Remaining .doubleValue( ) == 0)
{
certain operations
}
When i am doing so it is giving the 100 % accurate results.As 0 is Autboxed to 0.0 in java 1.5.
But JTest is not allowing me to commit this as it is giving a warning : Floating Point Comparison can produce unexpected results so avoid their comparison.

The alternate to this that i can think of is following :
[ code]

Number Quantity_Remaining = Boundary.getQuantity_remaining ;
if( Double.valueOf(Quantity_Remaining).equals(new Double(0))
{

}


I Just want to ask whether this comparison can produce unexpected results/incorrect comparison under any circumstances.Or is it 100 % safe to do this sort of comparison of double values.


Thanks !!!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 16 2009
Added on Jun 9 2009
8 comments
1,517 views