Calculator兼容x字符作为乘号(issue#3787@Github)

This commit is contained in:
Looly 2024-11-15 11:29:51 +08:00
parent 4a860c77ff
commit 62d31e4656
3 changed files with 18 additions and 2 deletions

View File

@ -2,11 +2,12 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.34(2024-11-13)
# 5.8.34(2024-11-15)
### 🐣新特性
* 【http 】 增加Windows微信浏览器识别issue#IB3SJF@Gitee
* 【core 】 ZipUtil.unzip增加编码容错issue#I3UZ28@Gitee
* 【core 】 Calculator兼容`x`字符作为乘号issue#3787@Github
### 🐞Bug修复
* 【core 】 修复DateUtil.rangeToList中step小于等于0时无限循环问题issue#3783@Github

View File

@ -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] == '(')) {

View File

@ -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);
}
}