* @throws NumberFormatException if the type cannot be represented as number.
*/
public Number convertToNumber(final Type sourceType, final Object value)
throws EvaluationException
{
final LocalizationContext localizationContext = context.getLocalizationContext();
if (value == null)
{
// there's no point in digging deeper - there *is* no value ..
throw TypeConversionException.getInstance();
}
final boolean isAnyType = sourceType.isFlagSet(Type.ANY_TYPE);
if (sourceType.isFlagSet(Type.NUMERIC_TYPE) || isAnyType)
{
if (sourceType.isFlagSet(Type.DATETIME_TYPE)
|| sourceType.isFlagSet(Type.TIME_TYPE)
|| sourceType.isFlagSet(Type.DATE_TYPE)
|| isAnyType)
{
if (value instanceof Date)
{
final BigDecimal serial = HSSFDateUtil.getExcelDate((Date) value);
return DateUtil.normalizeDate(serial, sourceType);
}
}
if (value instanceof Number)
{
return (Number) value;
}
}
if (sourceType.isFlagSet(Type.LOGICAL_TYPE) || isAnyType)
{
if (value instanceof Boolean)
{
if (Boolean.TRUE.equals(value))
{
return NUM_TRUE;
}
else
{
return NUM_FALSE;
}
}
}
if (sourceType.isFlagSet(Type.TEXT_TYPE) || isAnyType)
{
final String val = computeStringValue(value);
// first, try to parse the value as a big-decimal.
try
{
return new BigDecimal(val);
}
catch (NumberFormatException e)
{
// ignore ..
}
for (final DateFormat df : localizationContext.getDateFormats(DateTimeType.DATETIME_TYPE))
{
final Date date = parse(df, val);
if (date != null)
{
return HSSFDateUtil.getExcelDate(date);
}
}
for (final DateFormat df : localizationContext.getDateFormats(DateTimeType.DATE_TYPE))
{
final Date date = parse(df, val);
if (date != null)
{
return HSSFDateUtil.getExcelDate(date);
}
}
for (final DateFormat df : localizationContext.getDateFormats(DateTimeType.TIME_TYPE))
{
final Date date = parse(df, val);
if (date != null)
{
return HSSFDateUtil.getExcelDate(date);
}
}
// then checking for numbers
for (final NumberFormat format : localizationContext.getNumberFormats())
{
final Number number = parse(format, val);
if (number != null)
{
return number;