有效数字位修改
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -225,6 +230,10 @@ public class EffectiveNumberFormatter {
|
||||
// 如果 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,13 @@ public class EffectiveNumberFormatterTest {
|
||||
// 案例 B: 0.013500 -> 4位小数 0.0135 -> 小数2位有效 (1,3,遇5) -> 3是奇数,进位 -> 0.014
|
||||
test("0.013500", 4, 2, "0.0135 -> 3是奇数,遇5进位 -> 0.014");
|
||||
|
||||
System.out.println("\n=== 测试案例:后导零 ===");
|
||||
test("0.4", 4, 2, "0.4000 -> 保留两位有效数 -> 0.40");
|
||||
|
||||
test("0.004", 4, 2, "0.0040 -> 保留两位有效数 -> 0.0040");
|
||||
|
||||
test("0.00046", 4, 2, "0.00050 -> 超过小数精度 -> 0.0005");
|
||||
|
||||
System.out.println("\n=== 测试案例:多位前导零 ===");
|
||||
// 0.00012345 -> 6位小数 0.000123 -> 小数2位有效 (1,2,遇3舍去) -> 0.00012
|
||||
test("0.00012345", 6, 2, "0.000123 -> 0.00012");
|
||||
@@ -31,6 +38,30 @@ public class EffectiveNumberFormatterTest {
|
||||
|
||||
// 12345.00001234 -> 6位小数 12345.000012 -> 小数2位有效 -> 12345.000012
|
||||
test("12345.00001234", 6, 2, "前导零不计入有效位");
|
||||
|
||||
System.out.println("\n=== 测试案例:整体测试 ===");
|
||||
test("0.000456", 4, 2, "0.0005");
|
||||
test("0.00456", 4, 2, "0.0046");
|
||||
test("0.0456", 4, 2, "0.046");
|
||||
test("0.456", 4, 2, "0.46");
|
||||
test("0.4000", 4, 2, "0.40");
|
||||
test("0.0400", 4, 2, "0.040");
|
||||
test("0.0040", 4, 2, "0.0040");
|
||||
test("0.0004", 4, 2, "0.0004");
|
||||
test("0.000521", 4, 2, "0.0005");
|
||||
test("0.00521", 4, 2, "0.0052");
|
||||
test("0.0521", 4, 2, "0.052");
|
||||
test("0.5210", 4, 2, "0.52");
|
||||
test("0.1251", 4, 2, "0.13");
|
||||
test("0.01251", 4, 2, "0.013");
|
||||
test("0.001251", 4, 2, "0.0013");
|
||||
test("0.000125", 4, 2, "0.0001");
|
||||
test("0.4", 4, 2, "0.40");
|
||||
test("0.04", 4, 2, "0.040");
|
||||
test("0.004", 4, 2, "0.0040");
|
||||
test("0.0004", 4, 2, "0.0004");
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void test(String input, int d, int s, String desc) {
|
||||
|
||||
Reference in New Issue
Block a user