add methods

This commit is contained in:
Looly 2020-12-24 00:35:30 +08:00
parent 718c6f48ab
commit b8e4a131d8
9 changed files with 250 additions and 111 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.5.5 (2020-12-23) # 5.5.5 (2020-12-24)
### 新特性 ### 新特性
* 【core 】 URLUtil.normalize新增重载pr#233@Gitee * 【core 】 URLUtil.normalize新增重载pr#233@Gitee
@ -11,11 +11,14 @@
* 【db 】 RedisDS实现序列化接口pr#1323@Github * 【db 】 RedisDS实现序列化接口pr#1323@Github
* 【poi 】 StyleUtil增加getFormat方法pr#235@Gitee * 【poi 】 StyleUtil增加getFormat方法pr#235@Gitee
* 【poi 】 增加ExcelDateUtil更多日期格式支持issue#1316@Github * 【poi 】 增加ExcelDateUtil更多日期格式支持issue#1316@Github
* 【core 】 NumberUtil.toBigDecimal支持各类数字格式如1,234.56等issue#1334@Github
* 【core 】 NumberUtil增加parseXXX方法issue#1334@Github
### Bug修复 ### Bug修复
* 【core 】 FileUtil.isSub相对路径判断问题pr#1315@Github * 【core 】 FileUtil.isSub相对路径判断问题pr#1315@Github
* 【core 】 TreeUtil增加空判定issue#I2ACCW@Gitee * 【core 】 TreeUtil增加空判定issue#I2ACCW@Gitee
* 【db 】 解决Hive获取表名失败问题issue#I2AGLU@Gitee * 【db 】 解决Hive获取表名失败问题issue#I2AGLU@Gitee
* 【core 】 修复DateUtil.parse未使用严格模式导致结果不正常的问题issue#1332@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.5.4 (2020-12-16) # 5.5.4 (2020-12-16)

View File

@ -21,16 +21,16 @@ import java.util.function.Function;
* 数字转换器<br> * 数字转换器<br>
* 支持类型为<br> * 支持类型为<br>
* <ul> * <ul>
* <li><code>java.lang.Byte</code></li> * <li>{@code java.lang.Byte}</li>
* <li><code>java.lang.Short</code></li> * <li>{@code java.lang.Short}</li>
* <li><code>java.lang.Integer</code></li> * <li>{@code java.lang.Integer}</li>
* <li><code>java.util.concurrent.atomic.AtomicInteger</code></li> * <li>{@code java.util.concurrent.atomic.AtomicInteger}</li>
* <li><code>java.lang.Long</code></li> * <li>{@code java.lang.Long}</li>
* <li><code>java.util.concurrent.atomic.AtomicLong</code></li> * <li>{@code java.util.concurrent.atomic.AtomicLong}</li>
* <li><code>java.lang.Float</code></li> * <li>{@code java.lang.Float}</li>
* <li><code>java.lang.Double</code></li> * <li>{@code java.lang.Double}</li>
* <li><code>java.math.BigDecimal</code></li> * <li>{@code java.math.BigDecimal}</li>
* <li><code>java.math.BigInteger</code></li> * <li>{@code java.math.BigInteger}</li>
* </ul> * </ul>
* *
* @author Looly * @author Looly
@ -80,7 +80,11 @@ public class NumberConverter extends AbstractConverter<Number> {
return BooleanUtil.toByteObj((Boolean) value); return BooleanUtil.toByteObj((Boolean) value);
} }
final String valueStr = toStrFunc.apply(value); final String valueStr = toStrFunc.apply(value);
return StrUtil.isBlank(valueStr) ? null : Byte.valueOf(valueStr); try{
return StrUtil.isBlank(valueStr) ? null : Byte.valueOf(valueStr);
} catch (NumberFormatException e){
return NumberUtil.parseNumber(valueStr).byteValue();
}
} else if (Short.class == targetType) { } else if (Short.class == targetType) {
if (value instanceof Number) { if (value instanceof Number) {
return ((Number) value).shortValue(); return ((Number) value).shortValue();
@ -88,7 +92,11 @@ public class NumberConverter extends AbstractConverter<Number> {
return BooleanUtil.toShortObj((Boolean) value); return BooleanUtil.toShortObj((Boolean) value);
} }
final String valueStr = toStrFunc.apply((value)); final String valueStr = toStrFunc.apply((value));
return StrUtil.isBlank(valueStr) ? null : Short.valueOf(valueStr); try{
return StrUtil.isBlank(valueStr) ? null : Short.valueOf(valueStr);
} catch (NumberFormatException e){
return NumberUtil.parseNumber(valueStr).shortValue();
}
} else if (Integer.class == targetType) { } else if (Integer.class == targetType) {
if (value instanceof Number) { if (value instanceof Number) {
return ((Number) value).intValue(); return ((Number) value).intValue();
@ -146,8 +154,7 @@ public class NumberConverter extends AbstractConverter<Number> {
return BooleanUtil.toFloatObj((Boolean) value); return BooleanUtil.toFloatObj((Boolean) value);
} }
final String valueStr = toStrFunc.apply((value)); final String valueStr = toStrFunc.apply((value));
return StrUtil.isBlank(valueStr) ? null : Float.valueOf(valueStr); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseFloat(valueStr);
} else if (Double.class == targetType) { } else if (Double.class == targetType) {
if (value instanceof Number) { if (value instanceof Number) {
return ((Number) value).doubleValue(); return ((Number) value).doubleValue();
@ -155,7 +162,7 @@ public class NumberConverter extends AbstractConverter<Number> {
return BooleanUtil.toDoubleObj((Boolean) value); return BooleanUtil.toDoubleObj((Boolean) value);
} }
final String valueStr = toStrFunc.apply((value)); final String valueStr = toStrFunc.apply((value));
return StrUtil.isBlank(valueStr) ? null : Double.valueOf(valueStr); return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseDouble(valueStr);
} else if (DoubleAdder.class == targetType) { } else if (DoubleAdder.class == targetType) {
//jdk8 新增 //jdk8 新增
final Number number = convert(value, Long.class, toStrFunc); final Number number = convert(value, Long.class, toStrFunc);

View File

@ -219,7 +219,7 @@ public class DateTime extends Date {
* @see DatePattern * @see DatePattern
*/ */
public DateTime(CharSequence dateStr, String format) { public DateTime(CharSequence dateStr, String format) {
this(dateStr, new SimpleDateFormat(format)); this(dateStr, DateUtil.newSimpleFormat(format));
} }
/** /**
@ -895,9 +895,7 @@ public class DateTime extends Date {
*/ */
public String toString(TimeZone timeZone) { public String toString(TimeZone timeZone) {
if (null != timeZone) { if (null != timeZone) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN); return toString(DateUtil.newSimpleFormat(DatePattern.NORM_DATETIME_PATTERN, null, timeZone));
simpleDateFormat.setTimeZone(timeZone);
return toString(simpleDateFormat);
} }
return toString(DatePattern.NORM_DATETIME_FORMAT); return toString(DatePattern.NORM_DATETIME_FORMAT);
} }
@ -910,9 +908,7 @@ public class DateTime extends Date {
*/ */
public String toDateStr() { public String toDateStr() {
if (null != this.timeZone) { if (null != this.timeZone) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN); return toString(DateUtil.newSimpleFormat(DatePattern.NORM_DATE_PATTERN, null, timeZone));
simpleDateFormat.setTimeZone(this.timeZone);
return toString(simpleDateFormat);
} }
return toString(DatePattern.NORM_DATE_FORMAT); return toString(DatePattern.NORM_DATE_FORMAT);
} }
@ -925,9 +921,7 @@ public class DateTime extends Date {
*/ */
public String toTimeStr() { public String toTimeStr() {
if (null != this.timeZone) { if (null != this.timeZone) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DatePattern.NORM_TIME_PATTERN); return toString(DateUtil.newSimpleFormat(DatePattern.NORM_TIME_PATTERN, null, timeZone));
simpleDateFormat.setTimeZone(this.timeZone);
return toString(simpleDateFormat);
} }
return toString(DatePattern.NORM_TIME_FORMAT); return toString(DatePattern.NORM_TIME_FORMAT);
} }
@ -940,9 +934,7 @@ public class DateTime extends Date {
*/ */
public String toString(String format) { public String toString(String format) {
if (null != this.timeZone) { if (null != this.timeZone) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); return toString(DateUtil.newSimpleFormat(format, null, timeZone));
simpleDateFormat.setTimeZone(this.timeZone);
return toString(simpleDateFormat);
} }
return toString(FastDateFormat.getInstance(format)); return toString(FastDateFormat.getInstance(format));
} }

View File

@ -14,14 +14,18 @@ import cn.hutool.core.util.StrUtil;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.Year; import java.time.Year;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.*; import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -506,14 +510,11 @@ public class DateUtil extends CalendarUtil {
return null; return null;
} }
final SimpleDateFormat sdf = new SimpleDateFormat(format); TimeZone timeZone = null;
if (date instanceof DateTime) { if (date instanceof DateTime) {
final TimeZone timeZone = ((DateTime) date).getTimeZone(); timeZone = ((DateTime) date).getTimeZone();
if (null != timeZone) {
sdf.setTimeZone(timeZone);
}
} }
return format(date, sdf); return format(date, newSimpleFormat(format, null, timeZone));
} }
/** /**
@ -717,7 +718,7 @@ public class DateUtil extends CalendarUtil {
* @since 4.5.18 * @since 4.5.18
*/ */
public static DateTime parse(CharSequence dateStr, String format, Locale locale) { public static DateTime parse(CharSequence dateStr, String format, Locale locale) {
return new DateTime(dateStr, new SimpleDateFormat(format, locale)); return new DateTime(dateStr, DateUtil.newSimpleFormat(format, locale, null));
} }
/** /**
@ -1917,7 +1918,7 @@ public class DateUtil extends CalendarUtil {
/** /**
* 获得指定月份的总天数 * 获得指定月份的总天数
* *
* @param month 年份 * @param month 年份
* @param isLeapYear 是否闰年 * @param isLeapYear 是否闰年
* @return * @return
* @since 5.4.2 * @since 5.4.2
@ -1926,6 +1927,40 @@ public class DateUtil extends CalendarUtil {
return java.time.Month.of(month).length(isLeapYear); return java.time.Month.of(month).length(isLeapYear);
} }
/**
* 创建{@link SimpleDateFormat}注意此对象非线程安全<br>
* 此对象默认为严格格式模式即parse时如果格式不正确会报错
*
* @param pattern 表达式
* @return {@link SimpleDateFormat}
* @since 5.5.5
*/
public static SimpleDateFormat newSimpleFormat(String pattern) {
return newSimpleFormat(pattern, null, null);
}
/**
* 创建{@link SimpleDateFormat}注意此对象非线程安全<br>
* 此对象默认为严格格式模式即parse时如果格式不正确会报错
*
* @param pattern 表达式
* @param locale {@link Locale}{@code null}表示默认
* @param timeZone {@link TimeZone}{@code null}表示默认
* @return {@link SimpleDateFormat}
* @since 5.5.5
*/
public static SimpleDateFormat newSimpleFormat(String pattern, Locale locale, TimeZone timeZone) {
if (null == locale) {
locale = Locale.getDefault(Locale.Category.FORMAT);
}
final SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
if (null != timeZone) {
format.setTimeZone(timeZone);
}
format.setLenient(false);
return format;
}
// ------------------------------------------------------------------------ Private method start // ------------------------------------------------------------------------ Private method start
/** /**

View File

@ -1,5 +1,8 @@
package cn.hutool.core.date.format; package cn.hutool.core.date.format;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Tuple;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.Format; import java.text.Format;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -8,9 +11,6 @@ import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Tuple;
/** /**
* 日期格式化器缓存<br> * 日期格式化器缓存<br>
* Thanks to Apache Commons Lang 3.5 * Thanks to Apache Commons Lang 3.5
@ -43,7 +43,7 @@ abstract class FormatCache<F extends Format> {
* @param timeZone 时区默认当前时区 * @param timeZone 时区默认当前时区
* @param locale 地区默认使用当前地区 * @param locale 地区默认使用当前地区
* @return 格式化器 * @return 格式化器
* @throws IllegalArgumentException pattern 无效或<code>null</code> * @throws IllegalArgumentException pattern 无效或{@code null}
*/ */
public F getInstance(final String pattern, TimeZone timeZone, Locale locale) { public F getInstance(final String pattern, TimeZone timeZone, Locale locale) {
Assert.notBlank(pattern, "pattern must not be blank") ; Assert.notBlank(pattern, "pattern must not be blank") ;
@ -74,7 +74,7 @@ abstract class FormatCache<F extends Format> {
* @param timeZone 时区默认当前时区 * @param timeZone 时区默认当前时区
* @param locale 地区默认使用当前地区 * @param locale 地区默认使用当前地区
* @return 格式化器 * @return 格式化器
* @throws IllegalArgumentException pattern 无效或<code>null</code> * @throws IllegalArgumentException pattern 无效或{@code null}
*/ */
abstract protected F createInstance(String pattern, TimeZone timeZone, Locale locale); abstract protected F createInstance(String pattern, TimeZone timeZone, Locale locale);

View File

@ -2044,6 +2044,11 @@ public class NumberUtil {
* @since 4.0.9 * @since 4.0.9
*/ */
public static BigDecimal toBigDecimal(String number) { public static BigDecimal toBigDecimal(String number) {
try{
number = parseNumber(number).toString();
} catch (Exception ignore){
// 忽略解析错误
}
return StrUtil.isBlank(number) ? BigDecimal.ZERO : new BigDecimal(number); return StrUtil.isBlank(number) ? BigDecimal.ZERO : new BigDecimal(number);
} }
@ -2287,7 +2292,7 @@ public class NumberUtil {
* *
* <pre> * <pre>
* 10x开头的视为16进制数字 * 10x开头的视为16进制数字
* 20开头的视为8进制数字 * 20开头的忽略开头的0
* 3其它情况按照10进制转换 * 3其它情况按照10进制转换
* 4空串返回0 * 4空串返回0
* 5.123形式返回0按照小于0的小数对待 * 5.123形式返回0按照小于0的小数对待
@ -2304,18 +2309,16 @@ public class NumberUtil {
return 0; return 0;
} }
// 对于带小数转换为整数采取去掉小数的策略
number = StrUtil.subBefore(number, CharUtil.DOT, false);
if (StrUtil.isEmpty(number)) {
return 0;
}
if (StrUtil.startWithIgnoreCase(number, "0x")) { if (StrUtil.startWithIgnoreCase(number, "0x")) {
// 0x04表示16进制数 // 0x04表示16进制数
return Integer.parseInt(number.substring(2), 16); return Integer.parseInt(number.substring(2), 16);
} }
return Integer.parseInt(removeNumberFlag(number)); try{
return Integer.parseInt(number);
} catch (NumberFormatException e){
return parseNumber(number).intValue();
}
} }
/** /**
@ -2323,9 +2326,11 @@ public class NumberUtil {
* *
* <pre> * <pre>
* 10x开头的视为16进制数字 * 10x开头的视为16进制数字
* 20开头的视为8进制数字 * 20开头的忽略开头的0
* 3空串返回0 * 3空串返回0
* 4其它情况按照10进制转换 * 4其它情况按照10进制转换
* 5.123形式返回0按照小于0的小数对待
* 6123.56截取小数点之前的数字忽略小数部分
* </pre> * </pre>
* *
* @param number 数字支持0x开头0开头和普通十进制 * @param number 数字支持0x开头0开头和普通十进制
@ -2334,13 +2339,7 @@ public class NumberUtil {
*/ */
public static long parseLong(String number) { public static long parseLong(String number) {
if (StrUtil.isBlank(number)) { if (StrUtil.isBlank(number)) {
return 0; return 0L;
}
// 对于带小数转换为整数采取去掉小数的策略
number = StrUtil.subBefore(number, CharUtil.DOT, false);
if (StrUtil.isEmpty(number)) {
return 0;
} }
if (number.startsWith("0x")) { if (number.startsWith("0x")) {
@ -2348,7 +2347,63 @@ public class NumberUtil {
return Long.parseLong(number.substring(2), 16); return Long.parseLong(number.substring(2), 16);
} }
return Long.parseLong(removeNumberFlag(number)); try{
return Long.parseLong(number);
} catch (NumberFormatException e){
return parseNumber(number).longValue();
}
}
/**
* 解析转换数字字符串为long型数字规则如下
*
* <pre>
* 10开头的忽略开头的0
* 2空串返回0
* 3其它情况按照10进制转换
* 4.123形式返回0.123按照小于0的小数对待
* </pre>
*
* @param number 数字支持0x开头0开头和普通十进制
* @return long
* @since 5.5.5
*/
public static float parseFloat(String number) {
if (StrUtil.isBlank(number)) {
return 0f;
}
try{
return Float.parseFloat(number);
} catch (NumberFormatException e){
return parseNumber(number).floatValue();
}
}
/**
* 解析转换数字字符串为long型数字规则如下
*
* <pre>
* 10开头的忽略开头的0
* 2空串返回0
* 3其它情况按照10进制转换
* 4.123形式返回0.123按照小于0的小数对待
* </pre>
*
* @param number 数字支持0x开头0开头和普通十进制
* @return long
* @since 5.5.5
*/
public static double parseDouble(String number) {
if (StrUtil.isBlank(number)) {
return 0D;
}
try{
return Double.parseDouble(number);
} catch (NumberFormatException e){
return parseNumber(number).doubleValue();
}
} }
/** /**
@ -2357,13 +2412,15 @@ public class NumberUtil {
* @param numberStr Number字符串 * @param numberStr Number字符串
* @return Number对象 * @return Number对象
* @since 4.1.15 * @since 4.1.15
* @throws NumberFormatException 包装了{@link ParseException}当给定的数字字符串无法解析时抛出
*/ */
public static Number parseNumber(String numberStr) { public static Number parseNumber(String numberStr) throws NumberFormatException{
numberStr = removeNumberFlag(numberStr);
try { try {
return NumberFormat.getInstance().parse(numberStr); return NumberFormat.getInstance().parse(numberStr);
} catch (ParseException e) { } catch (ParseException e) {
throw new UtilException(e); final NumberFormatException nfe = new NumberFormatException(e.getMessage());
nfe.initCause(e);
throw nfe;
} }
} }
@ -2509,25 +2566,5 @@ public class NumberUtil {
return selectNum * mathNode(selectNum - 1); return selectNum * mathNode(selectNum - 1);
} }
} }
/**
* 去掉数字尾部的数字标识例如12D44.0F22L中的最后一个字母
*
* @param number 数字字符串
* @return 去掉标识的字符串
*/
private static String removeNumberFlag(String number) {
// 去掉千位分隔符
if (StrUtil.contains(number, CharUtil.COMMA)) {
number = StrUtil.removeAll(number, CharUtil.COMMA);
}
// 去掉类型标识的结尾
final int lastPos = number.length() - 1;
final char lastCharUpper = Character.toUpperCase(number.charAt(lastPos));
if ('D' == lastCharUpper || 'L' == lastCharUpper || 'F' == lastCharUpper) {
number = StrUtil.subPre(number, lastPos);
}
return number;
}
// ------------------------------------------------------------------------------------------- Private method end // ------------------------------------------------------------------------------------------- Private method end
} }

View File

@ -0,0 +1,22 @@
package cn.hutool.core.convert;
import cn.hutool.core.convert.impl.NumberConverter;
import org.junit.Assert;
import org.junit.Test;
public class NumberConverterTest {
@Test
public void toDoubleTest(){
final NumberConverter numberConverter = new NumberConverter(Double.class);
final Number convert = numberConverter.convert("1,234.55", null);
Assert.assertEquals(1234.55D, convert);
}
@Test
public void toIntegerTest(){
final NumberConverter numberConverter = new NumberConverter(Integer.class);
final Number convert = numberConverter.convert("1,234.55", null);
Assert.assertEquals(1234, convert);
}
}

View File

@ -831,4 +831,11 @@ public class DateUtilTest {
final DateTime parse = DateUtil.parse(dt); final DateTime parse = DateUtil.parse(dt);
Assert.assertEquals("2020-06-03 12:32:12", parse.toString()); Assert.assertEquals("2020-06-03 12:32:12", parse.toString());
} }
@Test(expected = DateException.class)
public void parseNotFitTest(){
//https://github.com/looly/hutool/issues/1332
// 在日期格式不匹配的时候测试是否正常报错
final DateTime parse = DateUtil.parse("2020-12-23", DatePattern.PURE_DATE_PATTERN);
}
} }

View File

@ -189,6 +189,12 @@ public class NumberUtilTest {
BigDecimal bigDecimal = NumberUtil.toBigDecimal(a); BigDecimal bigDecimal = NumberUtil.toBigDecimal(a);
Assert.assertEquals("3.14", bigDecimal.toString()); Assert.assertEquals("3.14", bigDecimal.toString());
bigDecimal = NumberUtil.toBigDecimal("1,234.55");
Assert.assertEquals("1234.55", bigDecimal.toString());
bigDecimal = NumberUtil.toBigDecimal("1,234.56D");
Assert.assertEquals("1234.56", bigDecimal.toString());
} }
@Test @Test
@ -205,21 +211,33 @@ public class NumberUtilTest {
@Test @Test
public void parseIntTest() { public void parseIntTest() {
int v1 = NumberUtil.parseInt("0xFF"); int number = NumberUtil.parseInt("0xFF");
Assert.assertEquals(255, v1); Assert.assertEquals(255, number);
int v2 = NumberUtil.parseInt("010");
Assert.assertEquals(10, v2);
int v3 = NumberUtil.parseInt("10");
Assert.assertEquals(10, v3);
int v4 = NumberUtil.parseInt(" ");
Assert.assertEquals(0, v4);
int v5 = NumberUtil.parseInt("10F");
Assert.assertEquals(10, v5);
int v6 = NumberUtil.parseInt("22.4D");
Assert.assertEquals(22, v6);
int v7 = NumberUtil.parseInt("0"); // 0开头
Assert.assertEquals(0, v7); number = NumberUtil.parseInt("010");
Assert.assertEquals(10, number);
number = NumberUtil.parseInt("10");
Assert.assertEquals(10, number);
number = NumberUtil.parseInt(" ");
Assert.assertEquals(0, number);
number = NumberUtil.parseInt("10F");
Assert.assertEquals(10, number);
number = NumberUtil.parseInt("22.4D");
Assert.assertEquals(22, number);
number = NumberUtil.parseInt("22.6D");
Assert.assertEquals(22, number);
number = NumberUtil.parseInt("0");
Assert.assertEquals(0, number);
number = NumberUtil.parseInt(".123");
Assert.assertEquals(0, number);
} }
@Test @Test
@ -236,22 +254,40 @@ public class NumberUtilTest {
// 千位分隔符去掉 // 千位分隔符去掉
int v1 = NumberUtil.parseNumber("1,482.00").intValue(); int v1 = NumberUtil.parseNumber("1,482.00").intValue();
Assert.assertEquals(1482, v1); Assert.assertEquals(1482, v1);
Number v2 = NumberUtil.parseNumber("1,482.00D");
Assert.assertEquals(1482L, v2);
} }
@Test @Test
public void parseLongTest() { public void parseLongTest() {
long v1 = NumberUtil.parseLong("0xFF"); long number = NumberUtil.parseLong("0xFF");
Assert.assertEquals(255L, v1); Assert.assertEquals(255, number);
long v2 = NumberUtil.parseLong("010");
Assert.assertEquals(10L, v2); // 0开头
long v3 = NumberUtil.parseLong("10"); number = NumberUtil.parseLong("010");
Assert.assertEquals(10L, v3); Assert.assertEquals(10, number);
long v4 = NumberUtil.parseLong(" ");
Assert.assertEquals(0L, v4); number = NumberUtil.parseLong("10");
long v5 = NumberUtil.parseLong("10F"); Assert.assertEquals(10, number);
Assert.assertEquals(10L, v5);
long v6 = NumberUtil.parseLong("22.4D"); number = NumberUtil.parseLong(" ");
Assert.assertEquals(22L, v6); Assert.assertEquals(0, number);
number = NumberUtil.parseLong("10F");
Assert.assertEquals(10, number);
number = NumberUtil.parseLong("22.4D");
Assert.assertEquals(22, number);
number = NumberUtil.parseLong("22.6D");
Assert.assertEquals(22, number);
number = NumberUtil.parseLong("0");
Assert.assertEquals(0, number);
number = NumberUtil.parseLong(".123");
Assert.assertEquals(0, number);
} }
@Test @Test