From 102548d22cc14309dff73252db9900a35248ffb5 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 20 Jun 2016 10:52:23 -0400 Subject: [PATCH] Fix decimaltype float constructor --- .../org/hl7/fhir/dstu3/model/DecimalType.java | 8 ++--- .../ca/uhn/fhir/model/DecimalTypeTest.java | 29 +++++++++++++++++++ src/changes/changes.xml | 5 ++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java diff --git a/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java index daab918558d..844572a8c01 100644 --- a/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java +++ b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/DecimalType.java @@ -74,7 +74,7 @@ public class DecimalType extends PrimitiveType implements Comparable * Constructor */ public DecimalType(long theValue) { - setValue(new BigDecimal(theValue)); + setValue(theValue); } /** @@ -148,21 +148,21 @@ public class DecimalType extends PrimitiveType implements Comparable * Sets a new value using an integer */ public void setValueAsInteger(int theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } /** * Sets a new value using a long */ public void setValue(long theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } /** * Sets a new value using a double */ public void setValue(double theValue) { - setValue(new BigDecimal(theValue)); + setValue(BigDecimal.valueOf(theValue)); } @Override diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java new file mode 100644 index 00000000000..ad96ca890fe --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/model/DecimalTypeTest.java @@ -0,0 +1,29 @@ +package ca.uhn.fhir.model; + +import static org.junit.Assert.*; + +import org.hl7.fhir.dstu3.model.DecimalType; +import org.junit.Test; + +public class DecimalTypeTest { + + @Test + public void testDoubleValue() { + DecimalType d = new DecimalType(1.2D); + assertEquals("1.2", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(1.2D); + assertEquals("1.2", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(10); + assertEquals("10", d.getValueAsString()); + + d = new DecimalType(); + d.setValue(10L); + assertEquals("10", d.getValueAsString()); + + } + +} diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 26d87c5638a..374ad48dc35 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -359,6 +359,11 @@ Server now supports the _at parameter (including multiple repetitions) for history operation + + DecimalType used BigDecimal constructor instead of valueOf method to + create a BigDecimal from a double, resulting in weird floating point + conversions. Thanks to Craig McClendon for reporting! +