Bigdecimal compare size

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.