转java 用bigdecimal实现 =IFERROR((AK3/AN3-1)/9*0.1+(AL3/AN3-1)/9*0.15+(AM3/AN3-1)/9*0.5+(AO3/AP3-1)/9*0.25,"-")
时间: 2023-11-03 09:04:39 浏览: 81
java代码-BigDecimal转String
以下是使用BigDecimal实现的Java代码:
```
import java.math.BigDecimal;
public class Calculation {
public static String calculate(BigDecimal ak, BigDecimal an, BigDecimal al, BigDecimal am, BigDecimal ao, BigDecimal ap) {
try {
BigDecimal result = new BigDecimal(0);
BigDecimal nine = new BigDecimal(9);
if (an.compareTo(BigDecimal.ZERO) == 0) {
return "-";
}
BigDecimal akDivideAn = ak.divide(an, 10, BigDecimal.ROUND_HALF_UP);
BigDecimal alDivideAn = al.divide(an, 10, BigDecimal.ROUND_HALF_UP);
BigDecimal amDivideAn = am.divide(an, 10, BigDecimal.ROUND_HALF_UP);
BigDecimal aoDivideAp = ao.divide(ap, 10, BigDecimal.ROUND_HALF_UP);
BigDecimal first = akDivideAn.subtract(BigDecimal.ONE).divide(nine, 10, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.1"));
BigDecimal second = alDivideAn.subtract(BigDecimal.ONE).divide(nine, 10, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.15"));
BigDecimal third = amDivideAn.subtract(BigDecimal.ONE).divide(nine, 10, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.5"));
BigDecimal fourth = aoDivideAp.subtract(BigDecimal.ONE).divide(nine, 10, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("0.25"));
result = first.add(second).add(third).add(fourth);
return result.toString();
} catch (Exception e) {
return "-";
}
}
public static void main(String[] args) {
BigDecimal ak = new BigDecimal("1.23");
BigDecimal an = new BigDecimal("4.56");
BigDecimal al = new BigDecimal("7.89");
BigDecimal am = new BigDecimal("10.12");
BigDecimal ao = new BigDecimal("13.45");
BigDecimal ap = new BigDecimal("16.78");
String result = calculate(ak, an, al, am, ao, ap);
System.out.println(result);
}
}
```
其中,`calculate`方法接收6个参数,分别是AK3、AN3、AL3、AM3、AO3和AP3的值,使用`BigDecimal`进行精确计算,并返回结果字符串。如果计算过程中出现异常,则返回"-"。在`main`方法中,我们可以设置这些参数的值,并调用`calculate`方法进行计算。
阅读全文