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