| 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
|
|
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
|
 |
 |
|
|
subject: Best way of finding the bigdecimal is greater than 1 ?
|
|
|