On the difference between equals and CompareTo in BigDecimal in Java _java

Source: Internet
Author: User

These two days in the processing of payments to check the problem occurred, there is a comparison of the amount I used the bigdecimal equals method to compare two amounts are equal, resulting in the amount of errors (such as 3.0 and 3.00 comparison, etc.).

"NOTE: The following are all based on the Sun JDK version 1.4.2 As an example, the other implementations are not necessarily consistent, please ignore the"

First look at the BigDecimal equals method:

public boolean equals (Object x) {
	if (!) ( x instanceof BigDecimal)) return
	  false;
	BigDecimal Xdec = (BigDecimal) x;

	return scale = = Xdec.scale && intval.equals (xdec.intval);
  }

You can see that the Euquals method of BigDecimal is to determine the type of data to compare, and whether the accuracy (scale) and the value (the BigInteger equals method) are consistent if the object type is consistent.

In fact Javadoc inside already wrote very clear: "Compares this BigDecimal with the specified Object for equality." Unlike CompareTo, this is considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 i s not equal to 2.00 when compared by this method). " I just didn't pay attention to it!

Take another look at the CompareTo method:

public int CompareTo (BigDecimal val) {/
	* optimization:would run fine without the next three lines/
	int Sigdiff = Signum ()-Val.signum ();
	if (sigdiff!= 0) return
	  (Sigdiff > 0? 1:-1);

	/* If signs match, scale and compare intvals
	/BigDecimal arg[] = new bigdecimal[2];
	Arg[0] = this;	Arg[1] = val;
	Matchscale (ARG);
	Return Arg[0].intval.compareto (Arg[1].intval);
  }

You can see that this method has a matchscale processing, which means that the object with low precision is converted to high precision and then compared (the same is the BigInteger CompareTo method), and the Matchscale is implemented as follows:

private static void Matchscale (Bigdecimal[] val) {
	if (Val[0].scale < Val[1].scale)
	  val[0] = val[0]. Setscale (Val[1].scale);
	else if (Val[1].scale < Val[0].scale)
	  val[1] = Val[1].setscale (Val[0].scale);
  

Do a simple test:

System.out.println (New BigDecimal ("1.2"). Equals (New BigDecimal ("1.20")); Output false

System.out.println (new BigDecimal ("1.2"). CompareTo (New BigDecimal ("1.20")) = = 0);//Output True 

Also notice that I'm bigdecimal in the construction of the above is a string, if the incoming is the number of types of words will have any results, you can test yourself, and then analyze the reasons:

System.out.println (New BigDecimal ("1.2"). Equals (New BigDecimal ("1.20")); Output false
System.out.println (new BigDecimal ("1.2"). CompareTo (New BigDecimal ("1.20")) = = 0);//Output True
    
System.out.println (new BigDecimal (1.2). Equals (New BigDecimal ("1.20")); What's the output?
System.out.println (new BigDecimal (1.2). CompareTo (New BigDecimal ("1.20")) = = 0); What's the output?
  
System.out.println (new BigDecimal (1.2). Equals (new BigDecimal (1.20)); What's the output?
System.out.println (new BigDecimal (1.2). CompareTo (New BigDecimal (1.20)) = = 0);//output is?

The final conclusion is: for bigdecimal size comparison, using the Equals method will not only compare the size of the value, but also compare the accuracy of the two objects, and the CompareTo method will not be more accurate, only the size of the value.

Finally despise yourself, with so many years of Java language, even basic common sense did not understand!

This article on the Java BigDecimal equals and CompareTo difference is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

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.