mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
fix(NumberUtil): parseInt逻辑先后修改
- https://gitee.com/dromara/hutool/issues/I800C9 - 十六进制中E被误判为科学技术法,修改判断先后处理
This commit is contained in:
parent
4771078cf4
commit
7cc7d045b5
@ -2556,16 +2556,16 @@ public class NumberUtil {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(StrUtil.containsIgnoreCase(number, "E")){
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", number));
|
||||
}
|
||||
|
||||
if (StrUtil.startWithIgnoreCase(number, "0x")) {
|
||||
// 0x04表示16进制数
|
||||
return Integer.parseInt(number.substring(2), 16);
|
||||
}
|
||||
|
||||
if(StrUtil.containsIgnoreCase(number, "E")){
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", number));
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(number);
|
||||
} catch (NumberFormatException e) {
|
||||
|
@ -273,8 +273,8 @@ public class NumberUtilTest {
|
||||
|
||||
@Test
|
||||
public void parseIntTest() {
|
||||
int number = NumberUtil.parseInt("0xFF");
|
||||
Assert.assertEquals(255, number);
|
||||
int number = NumberUtil.parseInt("0xFE");
|
||||
Assert.assertEquals(254, number);
|
||||
|
||||
// 0开头
|
||||
number = NumberUtil.parseInt("010");
|
||||
|
Loading…
Reference in New Issue
Block a user