mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Calculator兼容x
字符作为乘号(issue#3787@Github)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@@ -192,6 +193,9 @@ public class Calculator {
|
||||
arr[i] = '~';
|
||||
}
|
||||
}
|
||||
} else if(CharUtil.equals(arr[i], 'x', true)){
|
||||
// issue#3787 x转换为*
|
||||
arr[i] = '*';
|
||||
}
|
||||
}
|
||||
if (arr[0] == '~' && (arr.length > 1 && arr[1] == '(')) {
|
||||
|
@@ -1,8 +1,9 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CalculatorTest {
|
||||
|
||||
@Test
|
||||
@@ -55,4 +56,14 @@ public class CalculatorTest {
|
||||
final double calcValue = Calculator.conversion("(11+2)12");
|
||||
assertEquals(156D, calcValue, 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void issue3787Test() {
|
||||
final Calculator calculator1 = new Calculator();
|
||||
double result = calculator1.calculate("0+50/100x(1/0.5)");
|
||||
assertEquals(1D, result);
|
||||
|
||||
result = calculator1.calculate("0+50/100X(1/0.5)");
|
||||
assertEquals(1D, result);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user