関数 round を書いています: static float round(float number, precision){}
関数は次のように動作するはずです: round(12.3456f, 3) = 12.345
関数の私の定義は次のようになります。
public static float round(float value, int precision) {
float result;
if(precision <= 0){
throw new RuntimeException("Precision can not be zero or less");
}
int number = (int) power(10,precision);
value = value * number;
result = (float)Math.round(value)/number;
return result;
}
しかし、問題は、この関数の単体テスト ケースがパスしないことです。
public void mathTestNew() {
assertEquals("MathTest",12.341,OOTBFunctions.round(12.3416f,3));
}
結果は junit.framework.AssertionFailedError: MathTest expected:<12.341> but was:<12.342> です。
このエラーを克服する方法がわかりません。BigDecimal がこれに役立つかどうかはわかりません。