support hex

This commit is contained in:
Looly 2022-05-16 18:58:04 +08:00
parent a913c6e5ed
commit 95f00b8712
2 changed files with 14 additions and 4 deletions

View File

@ -2,7 +2,6 @@ package cn.hutool.core.math;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.math.Calculator;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
@ -34,8 +33,7 @@ import java.util.Set;
* </p>
* 相关介绍
* <ul>
* <li>http://www.oschina.net/code/snippet_563112_25237</li>
* <li>https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking</li>
* <li><a href="https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking">one-jdk7-bug-thinking</a></li>
* </ul>
*
* @author Looly
@ -2454,7 +2452,7 @@ public class NumberUtil {
return 0L;
}
if (number.startsWith("0x")) {
if(StrUtil.startWithIgnoreCase(number, "0x")){
// 0x04表示16进制数
return Long.parseLong(number.substring(2), 16);
}
@ -2528,6 +2526,11 @@ public class NumberUtil {
* @since 4.1.15
*/
public static Number parseNumber(final String numberStr) throws NumberFormatException {
if(StrUtil.startWithIgnoreCase(numberStr, "0x")){
// 0x04表示16进制数
return Long.parseLong(numberStr.substring(2), 16);
}
try {
final NumberFormat format = NumberFormat.getInstance();
if (format instanceof DecimalFormat) {

View File

@ -293,6 +293,13 @@ public class NumberUtilTest {
Assert.assertEquals(1482L, v2.longValue());
}
@Test
public void parseHexNumberTest() {
// 千位分隔符去掉
final int v1 = NumberUtil.parseNumber("0xff").intValue();
Assert.assertEquals(255, v1);
}
@Test
public void parseLongTest() {
long number = NumberUtil.parseLong("0xFF");