• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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?
 
Ranch Foreman
Posts: 3268
20
  • 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
Ranch Foreman
Posts: 3268
20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, here's another:
 
Bartender
Posts: 5908
57
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!