File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Best way of finding the bigdecimal is greater than 1 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Best way of finding the bigdecimal is greater than 1 ?" Watch "Best way of finding the bigdecimal is greater than 1 ?" New topic
Author

Best way of finding the bigdecimal is greater than 1 ?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 832
Snapshot of the code :
1)


2)


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I would say a variation of the first one. But don't create a new BigDecimal representation of 1 each time. Use the static BigDecimal ONE from the BigDecimal class (added in Java 1.5). Also, don't use the > 1.00 since that is confusing. The compareTo method returns one of three distinct values. Use them.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

Mark Vedder wrote:The compareTo method returns one of three distinct values. Use them.


Actually... the API documentation for that method says:
The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.

I would agree that's the most understandable way to write the code.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 832
Excellent Mark. Thanks
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Paul Clapham wrote:
Actually... the API documentation for that method says:
The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.

I would agree that's the most understandable way to write the code.


Good point Paul. Thanks. I think it was the comparison to 1.00 that was looking weird to me. Consistently comparing to 0 would indeed be clearer.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 832
OK using 0, will allow the use of 6 operators. I was wondering, how to use 6 operators with 1.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

x.compareTo(y) returns a positive number if x > y.

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Best way of finding the bigdecimal is greater than 1 ?
 
Similar Threads
XML to XML transformation
comparision between double and long
Rounding in BigDecimal
WebSphere Application Server 6.0.2.0 (Error 500)
Why isn't this generics based instantiation working?