How to use comparison operators like >, =, < on BigDecimal

后端 未结 8 1191
清酒与你
清酒与你 2021-01-30 11:54

I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can\'t have comparison operators in Big

8条回答
  •  被撕碎了的回忆
    2021-01-30 13:00

    Here is an example for all six boolean comparison operators (<, ==, >, >=, !=, <=):

    BigDecimal big10 = new BigDecimal(10);
    BigDecimal big20 = new BigDecimal(20);
    
    System.out.println(big10.compareTo(big20) < -1);  // false
    System.out.println(big10.compareTo(big20) <= -1); // true
    System.out.println(big10.compareTo(big20) == -1); // true
    System.out.println(big10.compareTo(big20) >= -1); // true
    System.out.println(big10.compareTo(big20) > -1);  // false
    System.out.println(big10.compareTo(big20) != -1); // false
    
    System.out.println(big10.compareTo(big20) < 0);   // true
    System.out.println(big10.compareTo(big20) <= 0);  // true
    System.out.println(big10.compareTo(big20) == 0);  // false
    System.out.println(big10.compareTo(big20) >= 0);  // false
    System.out.println(big10.compareTo(big20) > 0);   // false
    System.out.println(big10.compareTo(big20) != 0);  // true
    
    System.out.println(big10.compareTo(big20) < 1);   // true
    System.out.println(big10.compareTo(big20) <= 1);  // true
    System.out.println(big10.compareTo(big20) == 1);  // false
    System.out.println(big10.compareTo(big20) >= 1);  // false
    System.out.println(big10.compareTo(big20) > 1);   // false
    System.out.println(big10.compareTo(big20) != 1);  // true
    

提交回复
热议问题