mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix convert bug
This commit is contained in:
parent
5eac491cd4
commit
8a82424097
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.23 (2022-03-05)
|
||||
# 5.7.23 (2022-03-08)
|
||||
|
||||
### 🐣新特性
|
||||
* 【http 】 HttpRequest.form采用TableMap方式(issue#I4W427@Gitee)
|
||||
@ -11,6 +11,7 @@
|
||||
* 【crypto 】 增加XXTEA实现(issue#I4WH2X@Gitee)
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
||||
* 【core 】 修复NumberConverter对数字转换的问题(issue#I4WPF4@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.22 (2022-03-01)
|
||||
|
@ -77,6 +77,8 @@ import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.atomic.DoubleAdder;
|
||||
import java.util.concurrent.atomic.LongAdder;
|
||||
|
||||
/**
|
||||
* 转换器登记中心
|
||||
@ -389,11 +391,13 @@ public class ConverterRegistry implements Serializable {
|
||||
defaultConverterMap.put(Integer.class, new NumberConverter(Integer.class));
|
||||
defaultConverterMap.put(AtomicInteger.class, new NumberConverter(AtomicInteger.class));// since 3.0.8
|
||||
defaultConverterMap.put(Long.class, new NumberConverter(Long.class));
|
||||
defaultConverterMap.put(LongAdder.class, new NumberConverter(LongAdder.class));
|
||||
defaultConverterMap.put(AtomicLong.class, new NumberConverter(AtomicLong.class));// since 3.0.8
|
||||
defaultConverterMap.put(Byte.class, new NumberConverter(Byte.class));
|
||||
defaultConverterMap.put(Short.class, new NumberConverter(Short.class));
|
||||
defaultConverterMap.put(Float.class, new NumberConverter(Float.class));
|
||||
defaultConverterMap.put(Double.class, new NumberConverter(Double.class));
|
||||
defaultConverterMap.put(DoubleAdder.class, new NumberConverter(DoubleAdder.class));
|
||||
defaultConverterMap.put(Character.class, new CharacterConverter());
|
||||
defaultConverterMap.put(Boolean.class, new BooleanConverter());
|
||||
defaultConverterMap.put(AtomicBoolean.class, new AtomicBooleanConverter());// since 3.0.8
|
||||
|
@ -186,7 +186,7 @@ public class NumberConverter extends AbstractConverter<Number> {
|
||||
return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseFloat(valueStr);
|
||||
} else if (Double.class == targetType) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).doubleValue();
|
||||
return NumberUtil.toDouble((Number) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
return BooleanUtil.toDoubleObj((Boolean) value);
|
||||
}
|
||||
@ -194,7 +194,7 @@ public class NumberConverter extends AbstractConverter<Number> {
|
||||
return StrUtil.isBlank(valueStr) ? null : NumberUtil.parseDouble(valueStr);
|
||||
} else if (DoubleAdder.class == targetType) {
|
||||
//jdk8 新增
|
||||
final Number number = convert(value, Long.class, toStrFunc);
|
||||
final Number number = convert(value, Double.class, toStrFunc);
|
||||
if (null != number) {
|
||||
final DoubleAdder doubleAdder = new DoubleAdder();
|
||||
doubleAdder.add(number.doubleValue());
|
||||
|
@ -24,6 +24,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
import java.util.concurrent.atomic.DoubleAdder;
|
||||
|
||||
/**
|
||||
* 类型转换工具单元测试
|
||||
@ -361,4 +362,25 @@ public class ConvertTest {
|
||||
final float f = Convert.toFloat(value);
|
||||
Assert.assertEquals(406.1F, f, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void floatToDoubleTest(){
|
||||
float a = 0.45f;
|
||||
double b = Convert.toDouble(a);
|
||||
Assert.assertEquals(a, b, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void floatToDoubleAddrTest(){
|
||||
float a = 0.45f;
|
||||
final DoubleAdder adder = Convert.convert(DoubleAdder.class, a);
|
||||
Assert.assertEquals(a, adder.doubleValue(), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doubleToFloatTest(){
|
||||
double a = 0.45f;
|
||||
float b = Convert.toFloat(a);
|
||||
Assert.assertEquals(a, b, 5);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user