preface

Two days ago, I found a Bug in the test. I was told that he clearly only modified the quantity of goods in the details of the order. Why did he tell me that the unit price of goods in the order could not be modified (the background restricted that the unit price of special orders could not be modified), but he clearly did not modify the unit price of goods, which was very strange. I checked the input parameter and changed the unit price of the commodity transmitted through the interface to be price=222, and the unit price of the commodity in the database to be 222.000000. This field is of type BigDecimal and is equal except for its precision, and equals is used for background equality checks. The compareTo method is more commonly used for BigDecimal data lookup online.

Test both comparisons

public class BigDecimalDemo {

    public static void main(String [] args) {
        BigDecimal decimal1 = new BigDecimal("222");
        BigDecimal decimal2 = new BigDecimal("222.000000"); System.out.println(decimal1.equals(decimal2)); System.out.println(decimal1.compareTo(decimal2)); }}Copy the code

The results

Look at the source code

Equals method for BigDecimal

CompareTo method for BigDecimal

  • A < b, return 1
  • A is equal to b, return 0
  • A > b, returns 1

The public,

Follow the public account to read more exciting articles