Skip to content

Commit

Permalink
obiba/opal#3345 Do not convert double to string in scientific format
Browse files Browse the repository at this point in the history
  • Loading branch information
ymarcon committed Nov 19, 2018
1 parent 9ffe706 commit 34833fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@
import org.opendatafoundation.data.spss.SPSSStringVariable;
import org.opendatafoundation.data.spss.SPSSVariable;

import java.text.NumberFormat;

public class SpssVariableValueConverter {

private SpssVariableValueConverter() {}
private final NumberFormat fmt = NumberFormat.getInstance();

private SpssVariableValueConverter() {
fmt.setGroupingUsed(false);
fmt.setMaximumIntegerDigits(999);
fmt.setMaximumFractionDigits(999);
}

static SpssVariableValueConverter getInstance() {
return new SpssVariableValueConverter();
}

static String convert(SPSSVariable spssVariable, int index) throws SpssValueConversionException, SPSSFileException {
String convert(SPSSVariable spssVariable, int index) throws SpssValueConversionException, SPSSFileException {
if (spssVariable instanceof SPSSNumericVariable) {
SPSSNumericVariable spssNumVariable = (SPSSNumericVariable) spssVariable;
SpssNumericDataType spssNumericDataType = SpssVariableTypeMapper.getSpssNumericDataType(spssNumVariable);
Expand All @@ -32,7 +44,7 @@ static String convert(SPSSVariable spssVariable, int index) throws SpssValueConv
case FIXED: // fixed format (default)
case SCIENTIFIC:
Double doubleValue = spssNumVariable.getValue(index);
return doubleValue.isNaN() ? "" : "" + doubleValue;
return doubleValue.isNaN() ? "" : fmt.format(doubleValue);
}

}
Expand All @@ -41,7 +53,7 @@ static String convert(SPSSVariable spssVariable, int index) throws SpssValueConv
}


static String convert(SPSSVariable spssVariable, String value) throws SpssValueConversionException {
private String convert(SPSSVariable spssVariable, String value) throws SpssValueConversionException {
String trimmedValue = value.trim();
if(Strings.isNullOrEmpty(trimmedValue) || (spssVariable instanceof SPSSStringVariable)) return value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public SpssVariableValueFactory(List<Integer> valuesIndex, SPSSVariable spssVari
@Override
protected String getValue(int index) {
try {
return SpssVariableValueConverter.convert(spssVariable, index);
return SpssVariableValueConverter.getInstance().convert(spssVariable, index);
} catch(SPSSFileException | SpssValueConversionException e) {
String variableName = spssVariable.getName();
throw new SpssDatasourceParsingException("Failed to retieve variable value.", "SpssFailedToCreateVariable",
Expand Down

0 comments on commit 34833fe

Please sign in to comment.