mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
修复16进制解析错误的问题
This commit is contained in:
parent
880bd40870
commit
ead980e246
@ -106,16 +106,16 @@ public class NumberParser {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (StrUtil.containsIgnoreCase(numberStr, "E")) {
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", numberStr));
|
||||
}
|
||||
|
||||
if (StrUtil.startWithIgnoreCase(numberStr, "0x")) {
|
||||
// 0x04表示16进制数
|
||||
return Integer.parseInt(numberStr.substring(2), 16);
|
||||
}
|
||||
|
||||
if (StrUtil.containsIgnoreCase(numberStr, "E")) {
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", numberStr));
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(numberStr);
|
||||
} catch (final NumberFormatException e) {
|
||||
|
@ -393,6 +393,9 @@ public class NumberUtilTest {
|
||||
int number = NumberUtil.parseInt("0xFF");
|
||||
Assertions.assertEquals(255, number);
|
||||
|
||||
number = NumberUtil.parseInt("0xFE");
|
||||
Assertions.assertEquals(254, number);
|
||||
|
||||
// 0开头
|
||||
number = NumberUtil.parseInt("010");
|
||||
Assertions.assertEquals(10, number);
|
||||
|
Loading…
x
Reference in New Issue
Block a user