Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BigInteger support to NumberConverter #463

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading