JUnit Assert with BigDecimal

后端 未结 9 1998
遇见更好的自我
遇见更好的自我 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:20

    Other alternative for specific scale and rounded:

    import static org.assertj.core.api.Assertions.assertThat;
    
    ...
    
    BigDecimal a = new BigDecimal(100.05);
    BigDecimal b = new BigDecimal(100.048);
    
    a = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
    b = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);
    
    assertThat(a).isEqualTo(b);
    

提交回复
热议问题