add hex support

This commit is contained in:
Looly
2022-05-16 19:00:13 +08:00
parent 3115f0dad8
commit 0eb5c84ba8
3 changed files with 14 additions and 1 deletions

View File

@@ -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) {

View File

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