mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add hex support
This commit is contained in:
@@ -2524,6 +2524,11 @@ public class NumberUtil {
|
||||
* @since 4.1.15
|
||||
*/
|
||||
public static Number parseNumber(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) {
|
||||
|
@@ -292,6 +292,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");
|
||||
|
Reference in New Issue
Block a user