• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Checking for zero value ?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have these two methods available for checking whether it is zero or not.
which one is preferred or any new approach ?

I will prefer 1st if condition method.
 
author & internet detective
Posts: 39433
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither. Comparing a double to another double via == is unreliable. What if it is ".00000001"? It's better to do:
bd.doubleValue() - 0 < .0001 // or however much granularity you want

or:
BigDecimal.ZERO.equals(bd);
 
Sheriff
Posts: 21783
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:or:
BigDecimal.ZERO.equals(bd);


Output: false. This is because, scientifically speaking, 0 != 0.0.

prints 0 though, so that would be a possibility.
 
author
Posts: 23838
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Output: false. This is because, scientifically speaking, 0 != 0.0.



I was also surprised (a long while back), when I learned of this. It does seem weird.

Henry
 
Rob Spoor
Sheriff
Posts: 21783
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scientifically speaking, 0 can be any value between -0.5 (exclusive) and 0.5 (inclusive), whereas 0.0 can be any value between -0.05 (exclusive) and 0.05 (exclusive). If you take this into account it makes a bit more sense.
 
Jeanne Boyarsky
author & internet detective
Posts: 39433
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Rob Prime wrote:Output: false. This is because, scientifically speaking, 0 != 0.0.



I was also surprised (a long while back), when I learned of this. It does seem weird.

Henry


Interesting. Now I have to make sure I don't have this bug in any of my code!
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This already i learned from Rob in previous posts.

So the correct solution will be to use this code:

 
It's exactly the same and completely different as this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!