我正在写一个函数轮:静态浮点轮(浮点数,精度){}
该函数应该像这样工作: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 是否会在这方面帮助我。