有效数字位修改

This commit is contained in:
2026-02-28 19:37:57 +08:00
parent 4c29887ed9
commit ded289c402
3 changed files with 44 additions and 4 deletions

View File

@@ -773,7 +773,7 @@ public class SampleAnalysisAuditServiceImpl implements SampleAnalysisAuditServic
BigDecimal representativeValue = calculateRepresentativeValue(sortedValues, elementScale);
//计算后保留有效位数
representativeValue = EffectiveNumberFormatter.formatNumber(representativeValue, effectiveDigit);
representativeValue = EffectiveNumberFormatter.formatNumber2(representativeValue, elementScale, effectiveDigit);
//设置判定值
businessSubSampleAssessmentProjectDO.setAssessmentValue(representativeValue.toPlainString());
@@ -1326,7 +1326,7 @@ public class SampleAnalysisAuditServiceImpl implements SampleAnalysisAuditServic
e.printStackTrace();
}
//计算后保留有效位数
representativeValue = EffectiveNumberFormatter.formatNumber(representativeValue, effectiveDigit);
representativeValue = EffectiveNumberFormatter.formatNumber2(representativeValue, elementScale, effectiveDigit);
//设置判定值
businessSubSampleAssessmentProjectDO.setAssessmentValue(representativeValue.toPlainString());

View File

@@ -115,6 +115,11 @@ public class EffectiveNumberFormatter {
// 如果 newScale < currentScale, setScale 会截断并使用 RoundingMode
BigDecimal finalResult = step1.setScale(newScale, RoundingMode.HALF_EVEN);
// 6.如果小数位数大于精度,则在执行舍入
if (finalResult.scale() > decimalPlaces) {
finalResult = finalResult.setScale(decimalPlaces, RoundingMode.HALF_EVEN);
}
return finalResult.toPlainString();
/**
@@ -166,7 +171,7 @@ public class EffectiveNumberFormatter {
* @param effectiveDigit 要保留的有效数字位
* @return
*/
public static BigDecimal formatNumber(BigDecimal value, int effectiveDigit) {
public static BigDecimal formatNumber2(BigDecimal value, int decimalPlaces, int effectiveDigit) {
// 如果结果为0直接返回 (0.00...)
if (value.compareTo(BigDecimal.ZERO) == 0) {
@@ -224,7 +229,11 @@ public class EffectiveNumberFormatter {
// 注意:如果 newScale > currentScale, setScale 会补0
// 如果 newScale < currentScale, setScale 会截断并使用 RoundingMode
BigDecimal finalResult = value.setScale(newScale, RoundingMode.HALF_EVEN);
// 6.如果小数位数大于精度,则在执行舍入
if (finalResult.scale() > decimalPlaces) {
finalResult = finalResult.setScale(decimalPlaces, RoundingMode.HALF_EVEN);
}
return finalResult;
}