use of org.h2.value.ValueDecfloat in project 468H2Project by lukeunderwood42.
the class Value method convertToDecfloat.
private ValueDecfloat convertToDecfloat(TypeInfo targetType, int conversionMode) {
ValueDecfloat v;
switch(getValueType()) {
case DECFLOAT:
v = (ValueDecfloat) this;
if (v.value == null) {
return v;
}
break;
case CHAR:
case VARCHAR:
case VARCHAR_IGNORECASE:
{
String s = getString().trim();
try {
v = ValueDecfloat.get(new BigDecimal(s));
} catch (NumberFormatException e) {
switch(s) {
case "-Infinity":
return ValueDecfloat.NEGATIVE_INFINITY;
case "Infinity":
case "+Infinity":
return ValueDecfloat.POSITIVE_INFINITY;
case "NaN":
case "-NaN":
case "+NaN":
return ValueDecfloat.NAN;
default:
throw getDataConversionError(DECFLOAT);
}
}
break;
}
case BOOLEAN:
v = getBoolean() ? ValueDecfloat.ONE : ValueDecfloat.ZERO;
break;
case REAL:
{
float value = getFloat();
if (Float.isFinite(value)) {
v = ValueDecfloat.get(new BigDecimal(Float.toString(value)));
} else if (value == Float.POSITIVE_INFINITY) {
return ValueDecfloat.POSITIVE_INFINITY;
} else if (value == Float.NEGATIVE_INFINITY) {
return ValueDecfloat.NEGATIVE_INFINITY;
} else {
return ValueDecfloat.NAN;
}
break;
}
case DOUBLE:
{
double value = getDouble();
if (Double.isFinite(value)) {
v = ValueDecfloat.get(new BigDecimal(Double.toString(value)));
} else if (value == Double.POSITIVE_INFINITY) {
return ValueDecfloat.POSITIVE_INFINITY;
} else if (value == Double.NEGATIVE_INFINITY) {
return ValueDecfloat.NEGATIVE_INFINITY;
} else {
return ValueDecfloat.NAN;
}
break;
}
default:
try {
v = ValueDecfloat.get(getBigDecimal());
} catch (DbException e) {
if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) {
throw getDataConversionError(DECFLOAT);
}
throw e;
}
break;
case NULL:
throw DbException.getInternalError();
}
if (conversionMode != CONVERT_TO) {
BigDecimal bd = v.value;
int precision = bd.precision(), targetPrecision = (int) targetType.getPrecision();
if (precision > targetPrecision) {
v = ValueDecfloat.get(bd.setScale(bd.scale() - precision + targetPrecision, RoundingMode.HALF_UP));
}
}
return v;
}
Aggregations