修改有效位计算
This commit is contained in:
@@ -48,8 +48,8 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 处理 0 的特殊情况
|
||||
if (value.compareTo(BigDecimal.ZERO) == 0) {
|
||||
// 0 的有效数字通常视为 0,直接返回对应小数位的 0
|
||||
return value.setScale(decimalPlaces, RoundingMode.HALF_EVEN).toPlainString();
|
||||
// 0 的有效数字通常视为 0,直接返回对应有效小数位的 0
|
||||
return value.setScale(effectiveDigit, RoundingMode.HALF_EVEN).toPlainString();
|
||||
}
|
||||
|
||||
// --- 第一步:保留固定小数位 ---
|
||||
@@ -58,7 +58,7 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 如果结果为0,直接返回 (0.00...)
|
||||
if (step1.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return step1.toPlainString();
|
||||
return step1.setScale(effectiveDigit, RoundingMode.HALF_EVEN).toPlainString();
|
||||
}
|
||||
|
||||
// --- 第二步:针对小数部分保留有效数字 ---
|
||||
@@ -66,8 +66,8 @@ public class EffectiveNumberFormatter {
|
||||
// 1. 获取当前标度 (小数位数)
|
||||
int currentScale = step1.scale();
|
||||
if (currentScale <= 0) {
|
||||
// 如果没有小数部分,直接返回整数
|
||||
return step1.toPlainString();
|
||||
// 如果没有小数部分,直接返回有效小数位的0
|
||||
return step1.setScale(effectiveDigit, RoundingMode.HALF_EVEN).toPlainString();
|
||||
}
|
||||
|
||||
// 2. 将数字转换为纯小数字符串进行分析
|
||||
@@ -80,7 +80,7 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 如果第一步处理后小数部分为空或全0(理论上setScale后不会空,但可能全0)
|
||||
if (fractionalPart.isEmpty()) {
|
||||
return step1.toPlainString();
|
||||
return step1.setScale(effectiveDigit, RoundingMode.HALF_EVEN).toPlainString();
|
||||
}
|
||||
|
||||
// 3. 寻找小数部分第一个非零数字的位置
|
||||
@@ -92,9 +92,9 @@ public class EffectiveNumberFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果小数部分全是0 (例如 1.0000),则不需要再做有效数字截断,直接返回
|
||||
// 如果小数部分全是0 (例如 1.0000),直接返回有效小数位的0
|
||||
if (firstNonZeroIndex == -1) {
|
||||
return step1.toPlainString();
|
||||
return step1.setScale(effectiveDigit, RoundingMode.HALF_EVEN).toPlainString();
|
||||
}
|
||||
|
||||
// 4. 计算新的目标小数位数 (Scale)
|
||||
@@ -175,14 +175,14 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 如果结果为0,直接返回 (0.00...)
|
||||
if (value.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return value;
|
||||
return value.setScale(effectiveDigit, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
// 1. 获取当前标度 (小数位数)
|
||||
int currentScale = value.scale();
|
||||
if (currentScale <= 0) {
|
||||
// 如果没有小数部分,直接返回整数
|
||||
return value;
|
||||
return value.setScale(effectiveDigit, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
// 2. 将数字转换为纯小数字符串进行分析
|
||||
@@ -195,7 +195,7 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 如果第一步处理后小数部分为空或全0(理论上setScale后不会空,但可能全0)
|
||||
if (fractionalPart.isEmpty()) {
|
||||
return value;
|
||||
return value.setScale(effectiveDigit, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
// 3. 寻找小数部分第一个非零数字的位置
|
||||
@@ -209,7 +209,7 @@ public class EffectiveNumberFormatter {
|
||||
|
||||
// 如果小数部分全是0 (例如 1.0000),则不需要再做有效数字截断,直接返回
|
||||
if (firstNonZeroIndex == -1) {
|
||||
return value;
|
||||
return value.setScale(effectiveDigit, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
// 4. 计算新的目标小数位数 (Scale)
|
||||
|
||||
@@ -60,10 +60,17 @@ public class EffectiveNumberFormatterTest {
|
||||
test("0.04", 4, 2, "0.040");
|
||||
test("0.004", 4, 2, "0.0040");
|
||||
test("0.0004", 4, 2, "0.0004");
|
||||
test("1", 4, 2, "1.00");
|
||||
test("0", 4, 2, "0.00");
|
||||
|
||||
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void formatNumberTest2() {
|
||||
// test("1", 4, 2, "1.00");
|
||||
// }
|
||||
|
||||
private void test(String input, int d, int s, String desc) {
|
||||
try {
|
||||
String res = EffectiveNumberFormatter.formatNumber(input, d, s);
|
||||
|
||||
Reference in New Issue
Block a user