Skip to content

Commit

Permalink
Adds BigInteger support to NumberConverter (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelkaeser authored May 1, 2024
1 parent f37e651 commit fc4890e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/concepts/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ automatically be converted from one of the following Java data types:
- byte
- double
- float
- java.math.BigInteger

Be careful when passing double or float values, as these have to be converted from their
floating-point arithmetic representation to a BigDecimal representation, which stores values a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
import java.math.BigDecimal;
import java.math.BigInteger;

/** Converter to convert to the NUMBER data type. */
public class NumberConverter implements ConverterIfc {
Expand All @@ -28,6 +29,8 @@ public EvaluationValue convert(Object object, ExpressionConfiguration configurat

if (object instanceof BigDecimal) {
bigDecimal = (BigDecimal) object;
} else if (object instanceof BigInteger) {
bigDecimal = new BigDecimal((BigInteger) object, configuration.getMathContext());
} else if (object instanceof Double) {
bigDecimal = new BigDecimal(Double.toString((double) object), configuration.getMathContext());
} else if (object instanceof Float) {
Expand All @@ -50,6 +53,7 @@ public EvaluationValue convert(Object object, ExpressionConfiguration configurat
@Override
public boolean canConvert(Object object) {
return (object instanceof BigDecimal
|| object instanceof BigInteger
|| object instanceof Double
|| object instanceof Float
|| object instanceof Integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.jupiter.api.Test;

class NumberConverterTest {
Expand All @@ -40,6 +41,16 @@ void testBigDecimal() {
assertThat(converted.getValue()).isEqualTo(value);
}

@Test
void testBigInteger() {
BigInteger value = new BigInteger("23");

EvaluationValue converted = converter.convert(value, defaultConfiguration);

assertThat(converted.getDataType()).isEqualTo(EvaluationValue.DataType.NUMBER);
assertThat(converted.getNumberValue().toPlainString()).isEqualTo("23");
}

@Test
void testDouble() {
double value = Double.parseDouble("2.5");
Expand Down Expand Up @@ -99,6 +110,7 @@ void testByte() {
@Test
void testCanConvert() {
assertThat(converter.canConvert(new BigDecimal(8))).isTrue();
assertThat(converter.canConvert(new BigInteger("5"))).isTrue();
assertThat(converter.canConvert(Double.parseDouble("3.0"))).isTrue();
assertThat(converter.canConvert(Float.parseFloat("2.0"))).isTrue();
assertThat(converter.canConvert(3)).isTrue();
Expand Down

0 comments on commit fc4890e

Please # to comment.