Commit af6ff871 by ghxdhr

使用BigDecimal修约

parent 6df578c4
...@@ -144,29 +144,19 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -144,29 +144,19 @@ public class SingleSheetMoreItemOperation implements Operation {
String mapKey = config.getDataAttribute().substring(4); String mapKey = config.getDataAttribute().substring(4);
if (sampleIdMap.get(vo.getId()) != null && sampleIdMap.get(vo.getId()).size() > 0) { if (sampleIdMap.get(vo.getId()) != null && sampleIdMap.get(vo.getId()).size() > 0) {
if (null != config.getRoundNum()) { // 需要修约 if (null != config.getRoundNum()) { // 需要修约
// ROUND(A,小数位数)-(MOD(A*10^(小数位数+1),20)=5)*10^(-小数位数)
String formula = "ROUND(%s,%s)-(MOD(%s*10^(%s+1),20)=5)*10^(-%s)";
String s = sampleIdMap.get(vo.getId()).get(mapKey); String s = sampleIdMap.get(vo.getId()).get(mapKey);
if (needUseFormula(cell,s,config.getRoundNum())) { rounding(cell,s);
roundingValueUseFormula(cell,s,formula,config.getRoundNum());
}
if (null != cell2 && vo.getCollectionDataList().size() >= 2) { if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
String s1 = vo.getCollectionDataList().get(1).get(mapKey); String s1 = vo.getCollectionDataList().get(1).get(mapKey);
if (needUseFormula(cell2,s1,config.getRoundNum())) { rounding(cell2,s1);
roundingValueUseFormula(cell2,s1,formula,config.getRoundNum());
}
} }
if (null != cell3 && vo.getCollectionDataList().size() >= 3) { if (null != cell3 && vo.getCollectionDataList().size() >= 3) {
String s2 = vo.getCollectionDataList().get(2).get(mapKey); String s2 = vo.getCollectionDataList().get(2).get(mapKey);
if (needUseFormula(cell2,s2,config.getRoundNum())) { rounding(cell3,s2);
roundingValueUseFormula(cell3,s2,formula,config.getRoundNum());
}
} }
if (null != cell4 && vo.getCollectionDataList().size() >= 4) { if (null != cell4 && vo.getCollectionDataList().size() >= 4) {
String s3 = vo.getCollectionDataList().get(3).get(mapKey); String s3 = vo.getCollectionDataList().get(3).get(mapKey);
if (needUseFormula(cell4,s3,config.getRoundNum())) { rounding(cell4,s3);
roundingValueUseFormula(cell4,s3,formula,config.getRoundNum());
}
} }
continue; continue;
} }
...@@ -247,72 +237,15 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -247,72 +237,15 @@ public class SingleSheetMoreItemOperation implements Operation {
} }
} }
// 四舍六入五成双
/** private static void rounding(Cell cell, String value) {
* if (StringUtils.isNotBlank(value) && StringHandleUtils.validateNumber(value)) {
* @param cell BigDecimal bd1 = new BigDecimal(value);
* @param value BigDecimal bd2 = bd1.setScale(2, BigDecimal.ROUND_HALF_EVEN);
* @param roundNum 保留几位小数 cell.setCellValue(bd2.toPlainString());
* @return
*/
// 是否需要使用公式修约
private boolean needUseFormula(Cell cell, String value, Integer roundNum) {
if (StringUtils.isEmpty(value)) {
return false;
}
if (!StringHandleUtils.validateNumber(value)) {
cell.setCellValue("");
return false;
}
if (!value.contains(".")) {
cell.setCellValue(value);
return false;
}
int point = value.indexOf(".");
// 小数点后的数字
String stringAfterPoint = value.substring(point + 1);
if (stringAfterPoint.length() < roundNum) { // 少的位数补0
for (int i = 0; i < roundNum - stringAfterPoint.length(); i++) {
value = value + "0";
}
cell.setCellValue(value);
return false;
}
if (stringAfterPoint.length() == roundNum) {
cell.setCellValue(value);
return false;
}
char c = value.charAt(point + roundNum + 1);
char[] chars = {c};
int i = Integer.parseInt(new String(chars));
String afterPointRoundNum = value.substring(0, point + roundNum + 1);
if (i >= 6) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
if (i <= 4) {
cell.setCellValue(afterPointRoundNum);
return false;
}
String substring = value.substring(point + roundNum + 2);
if (Integer.parseInt(substring) > 0) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
} else { } else {
return true; cell.setCellValue("");
}
} }
// 使用公式进行修约
private void roundingValueUseFormula(Cell cell, String value, String formula, Integer roundNum) {
String format = String.format(formula, value, roundNum, value, roundNum, roundNum);
try {
cell.setCellFormula(format);
formulaEvaluator.evaluate(cell).getNumberValue();
} catch (FormulaParseException e) {
System.out.println("excel公式解析出错!");
} }
}
} }
\ No newline at end of file
...@@ -124,29 +124,19 @@ public class SingleSheetMoreOperation implements Operation { ...@@ -124,29 +124,19 @@ public class SingleSheetMoreOperation implements Operation {
if (CollectionUtils.isNotEmpty(vo.getCollectionDataList())) { if (CollectionUtils.isNotEmpty(vo.getCollectionDataList())) {
String s = vo.getCollectionDataList().get(0).get(mapKey); String s = vo.getCollectionDataList().get(0).get(mapKey);
if (null != config.getRoundNum()) { // 需要修约 if (null != config.getRoundNum()) { // 需要修约
// ROUND(A,小数位数)-(MOD(A*10^(小数位数+1),20)=5)*10^(-小数位数) rounding(cell,s);
String formula = "ROUND(%s,%s)-(MOD(%s*10^(%s+1),20)=5)*10^(-%s)";
if (needUseFormula(cell,s,config.getRoundNum())) {
roundingValueUseFormula(cell,s,formula,config.getRoundNum());
}
/* 下面为冗余代码,可能要删掉 */ /* 下面为冗余代码,可能要删掉 */
if (null != cell2 && vo.getCollectionDataList().size() >= 2) { if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
String s1 = vo.getCollectionDataList().get(1).get(mapKey); String s1 = vo.getCollectionDataList().get(1).get(mapKey);
if (needUseFormula(cell2,s1,config.getRoundNum())) { rounding(cell2,s1);
roundingValueUseFormula(cell2,s1,formula,config.getRoundNum());
}
} }
if (null != cell3 && vo.getCollectionDataList().size() >= 3) { if (null != cell3 && vo.getCollectionDataList().size() >= 3) {
String s2 = vo.getCollectionDataList().get(2).get(mapKey); String s2 = vo.getCollectionDataList().get(2).get(mapKey);
if (needUseFormula(cell2,s2,config.getRoundNum())) { rounding(cell3,s2);
roundingValueUseFormula(cell3,s2,formula,config.getRoundNum());
}
} }
if (null != cell4 && vo.getCollectionDataList().size() >= 4) { if (null != cell4 && vo.getCollectionDataList().size() >= 4) {
String s3 = vo.getCollectionDataList().get(3).get(mapKey); String s3 = vo.getCollectionDataList().get(3).get(mapKey);
if (needUseFormula(cell4,s3,config.getRoundNum())) { rounding(cell4,s3);
roundingValueUseFormula(cell4,s3,formula,config.getRoundNum());
}
} }
continue; continue;
} }
...@@ -270,71 +260,14 @@ public class SingleSheetMoreOperation implements Operation { ...@@ -270,71 +260,14 @@ public class SingleSheetMoreOperation implements Operation {
} }
} }
/** // 四舍六入五成双
* private static void rounding(Cell cell, String value) {
* @param cell if (StringUtils.isNotBlank(value) && StringHandleUtils.validateNumber(value)) {
* @param value BigDecimal bd1 = new BigDecimal(value);
* @param roundNum 保留几位小数 BigDecimal bd2 = bd1.setScale(2, BigDecimal.ROUND_HALF_EVEN);
* @return cell.setCellValue(bd2.toPlainString());
*/
// 是否需要使用公式修约
private boolean needUseFormula(Cell cell, String value, Integer roundNum) {
if (StringUtils.isEmpty(value)) {
return false;
}
if (!StringHandleUtils.validateNumber(value)) {
cell.setCellValue("");
return false;
}
if (!value.contains(".")) {
cell.setCellValue(value);
return false;
}
int point = value.indexOf(".");
// 小数点后的数字
String stringAfterPoint = value.substring(point + 1);
if (stringAfterPoint.length() < roundNum) { // 少的位数补0
for (int i = 0; i < roundNum - stringAfterPoint.length(); i++) {
value = value + "0";
}
cell.setCellValue(value);
return false;
}
if (stringAfterPoint.length() == roundNum) {
cell.setCellValue(value);
return false;
}
char c = value.charAt(point + roundNum + 1);
char[] chars = {c};
int i = Integer.parseInt(new String(chars));
String afterPointRoundNum = value.substring(0, point + roundNum + 1);
if (i >= 6) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
if (i <= 4) {
cell.setCellValue(afterPointRoundNum);
return false;
}
String substring = value.substring(point + roundNum + 2);
if (Integer.parseInt(substring) > 0) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
} else { } else {
return true; cell.setCellValue("");
}
}
// 使用公式进行修约
private void roundingValueUseFormula(Cell cell, String value, String formula, Integer roundNum) {
String format = String.format(formula, value, roundNum, value, roundNum, roundNum);
try {
cell.setCellFormula(format);
formulaEvaluator.evaluate(cell).getNumberValue();
} catch (FormulaParseException e) {
System.out.println("excel公式解析出错!");
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment