JUnit Assert with BigDecimal

后端 未结 9 2006
遇见更好的自我
遇见更好的自我 2021-02-02 05:47

I want to use assert between 2 two decimal, I use this:

BigDecimal bd1 = new BigDecimal (1000);
BigDecimal bd2 = new BigDecimal (1000);
org.junit.Assert.assertSa         


        
9条回答
  •  盖世英雄少女心
    2021-02-02 06:30

    assertSamechecks if both objects are the same instance. assertEqualschecks if the numbers are equal in value and scale, that means i.e. 1000 is not equal to 1000.00. If you want to compare only the numeric value, you should use compareTo() method from BigDecimal.

    For example:

    BigDecimal bd1 = new BigDecimal (1000.00);
    BigDecimal bd2 = new BigDecimal (1000);
    org.junit.Assert.assertTrue(bd1.compareTo(bd2) == 0); 
    

提交回复
热议问题