AssertJ 比较双精度值

Mic*_*ner 5 double compare offset assertj

我想将双精度值与 AssertJ 进行比较。我不明白为什么我的测试失败。

@Test
public void testInterestToQuote() {
    double result = BasicCalculator.accumulationFactorByYearsAndInterest(years, interest)
    Assertions.assertThat(result).isCloseTo(expected, Assertions.offset(0.1d))
}
Run Code Online (Sandbox Code Playgroud)

例外是:

java.lang.AssertionError: 
Expecting:
   <7.256571590148141E-5>
to be close to:
   <7.25>
by less than <0.1> but difference was <7.249927434284099>.
(a difference of exactly <0.1> being considered valid)
Run Code Online (Sandbox Code Playgroud)

为什么断言失败?

Mic*_*ner 2

昨天很晚了,但是7.256571590148141E-5并不像我想象的那样,7.25..,因为E-5将点5向左移动了,所以是0.0000725...

  • 如果其他人正在寻找具有一定精度的assertj小数点比较并最终出现在这里。正确的方法是这样的:`assertThat(result).isEqualTo(expected, withPrecision(2d)​​)` (11认同)