mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
support hex
This commit is contained in:
parent
a913c6e5ed
commit
95f00b8712
@ -2,7 +2,6 @@ package cn.hutool.core.math;
|
|||||||
|
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.math.Calculator;
|
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.CharUtil;
|
import cn.hutool.core.util.CharUtil;
|
||||||
@ -34,8 +33,7 @@ import java.util.Set;
|
|||||||
* </p>
|
* </p>
|
||||||
* 相关介绍:
|
* 相关介绍:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>http://www.oschina.net/code/snippet_563112_25237</li>
|
* <li><a href="https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking">one-jdk7-bug-thinking</a></li>
|
||||||
* <li>https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking</li>
|
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
@ -2454,7 +2452,7 @@ public class NumberUtil {
|
|||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (number.startsWith("0x")) {
|
if(StrUtil.startWithIgnoreCase(number, "0x")){
|
||||||
// 0x04表示16进制数
|
// 0x04表示16进制数
|
||||||
return Long.parseLong(number.substring(2), 16);
|
return Long.parseLong(number.substring(2), 16);
|
||||||
}
|
}
|
||||||
@ -2528,6 +2526,11 @@ public class NumberUtil {
|
|||||||
* @since 4.1.15
|
* @since 4.1.15
|
||||||
*/
|
*/
|
||||||
public static Number parseNumber(final String numberStr) throws NumberFormatException {
|
public static Number parseNumber(final String numberStr) throws NumberFormatException {
|
||||||
|
if(StrUtil.startWithIgnoreCase(numberStr, "0x")){
|
||||||
|
// 0x04表示16进制数
|
||||||
|
return Long.parseLong(numberStr.substring(2), 16);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final NumberFormat format = NumberFormat.getInstance();
|
final NumberFormat format = NumberFormat.getInstance();
|
||||||
if (format instanceof DecimalFormat) {
|
if (format instanceof DecimalFormat) {
|
||||||
|
@ -293,6 +293,13 @@ public class NumberUtilTest {
|
|||||||
Assert.assertEquals(1482L, v2.longValue());
|
Assert.assertEquals(1482L, v2.longValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseHexNumberTest() {
|
||||||
|
// 千位分隔符去掉
|
||||||
|
final int v1 = NumberUtil.parseNumber("0xff").intValue();
|
||||||
|
Assert.assertEquals(255, v1);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseLongTest() {
|
public void parseLongTest() {
|
||||||
long number = NumberUtil.parseLong("0xFF");
|
long number = NumberUtil.parseLong("0xFF");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user