Bigdecimal compare size
This class is the exact calculation class in Java. The following describes how to determine the size and equality of two bigdecimal objects.
1. Compare whether objects are equal
Generally, equals is used for objects, but bigdecimal is special. For example:
Bigdecimal A = bigdecimal. valueof (1.0 );
Bigdecimal B = bigdecimal. valueof (1.000 );
In reality, the two numbers are equal, but the problem arises.
A. Equals (B) returns false. Why is it not equal? I do not know this question.
Solution:
If (A. compareto (B) = 0), the result is true.
Public int compareto (bigdecimal Val)
Compares this bigdecimal with the specified bigdecimal.
Two bigdecimal objects that are equal in value but have a different
Scale (like 2.0 and 2.00) are considered equal by this method.
This method is provided in preference to individual methods for each
The six Boolean comparison operators (<,=, >,>= ,! =, <= ).
The suggested idiom for faster Ming these comparisons is: (X. compareto (y) <OP> 0), where <OP> is one of the six comparison operators.
Specified:
Compareto in interface comparable <bigdecimal>
Parameters:
Val-bigdecimal to which this bigdecimal is to be compared.
Returns:
-1, 0, or 1 as this bigdecimal is numerically less than, equal to, or greater than Val.
Simple translation:
The result is-1 less
0 equals
1>
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ylh2003/archive/2006/12/04/1429337.aspx
The above is an article found online.
If (A. compareto (B) =-1 ){
// A <B
}
If (A. compareto (B) = 0 ){
// A = B;
}
If (A. compareto (B) = 1 ){
// A> B;
}