Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

BigDecimal comparison between 2 values w and w/o 0

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've external data read in to my java program and there is a value for example 23.4 which I create a BigDecimal value of how ever further up in the program when doing comparison it will fails because 23.4 and 23.40 aren't the same.
What is the best way to have this fixed? Should I add a 0 to 23.4 so its new value will become 23.40 ? or should I use the compareTo() method to ignore 0 ? Are there any other solutions?
 
Rancher
Posts: 3371
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's the best solution - check if bigDecimalA.compareTo(bigDecimalB) == 0, if true that means they're equal.  There are other things you could do, like check if check if bigDecimalA.doubleValue() == bigDecimalB.doubleValue().  That will give you the right answer as long as your numbers don't use too many digits.  But the first one seems more reliable.
 
Mike Simmons
Rancher
Posts: 3371
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, here's another:
 
Saloon Keeper
Posts: 6246
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output:
23.4 compareTo(23.40) = 0
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I'll use the compareTo method.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!