Skip to content

Commit

Permalink
fix(转换工具): 超大精度的数字转换返回BigDecimal类型 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
bestfeng1020 authored Aug 1, 2024
1 parent af30e4c commit e4a3d9a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.0</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jetlinks/reactor/ql/utils/CastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public static Number castNumber(Object value, Function<Object, Number> fallback)
}
try {
BigDecimal decimal = new BigDecimal(stringValue);
if (decimal.precision() >= 17) {
return decimal;
}
if (decimal.scale() == 0) {
return decimal.longValue();
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/java/org/jetlinks/reactor/ql/utils/CastUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import java.math.BigDecimal;
import java.time.*;
import java.time.temporal.ChronoField;
import java.util.*;
Expand Down Expand Up @@ -70,7 +71,7 @@ void testCollection() {
assertEquals(CastUtils.castCollection(new Object[]{1, 2, 3}, new ArrayList<>()),
Arrays.asList(1, 2, 3));

assertEquals(CastUtils.castCollection(1,new ArrayList<>()), Collections.singletonList(1));
assertEquals(CastUtils.castCollection(1, new ArrayList<>()), Collections.singletonList(1));

}

Expand Down Expand Up @@ -153,6 +154,20 @@ void testCastNumber() {
Number.class::cast,
Number.class::cast
));
assertInstanceOf(BigDecimal.class, CastUtils.castNumber("1000000000000000000000000000000000"));
assertInstanceOf(Long.class, CastUtils.castNumber("1000000000000000"));
assertInstanceOf(Double.class, CastUtils.castNumber("0.000000000000000000000000000000001"));
assertInstanceOf(BigDecimal.class, CastUtils.castNumber("111111111111111111111111111111111.123"));
assertInstanceOf(Double.class, CastUtils.castNumber("1111111111111.123"));

assertEquals(new BigDecimal("1000000000000000000000000000000000"),
CastUtils.castNumber("1000000000000000000000000000000000"));

assertEquals(new Double("0.000000000000000000000000000000001"),
CastUtils.castNumber("0.000000000000000000000000000000001"));

assertEquals(new BigDecimal("111111111111111111111111111111111.123"),
CastUtils.castNumber("111111111111111111111111111111111.123"));
}

@Test
Expand Down

0 comments on commit e4a3d9a

Please sign in to comment.