Better error catching in toNumber function.

Watch out for the string "Infinity" while importing data sets: don't parse it into a double.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@438 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-04-09 21:59:50 +00:00
parent 8950e87e02
commit 70449cf7c8
2 changed files with 15 additions and 2 deletions

View File

@ -5,13 +5,23 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.Function; import com.metaweb.gridworks.gel.Function;
public class ToNumber implements Function { public class ToNumber implements Function {
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) { if (args.length == 1 && args[0] != null) {
return args[0] instanceof Number ? args[0] : Double.parseDouble(args[0].toString()); if (args[0] instanceof Number) {
return args[0];
} else {
String s = args[0].toString();
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
return new EvalError("Cannot parse to number");
}
}
} }
return null; return null;
} }

View File

@ -16,7 +16,10 @@ public class ImporterUtilities {
} }
try { try {
return Double.parseDouble(text); double d = Double.parseDouble(text);
if (!Double.isInfinite(d) && !Double.isNaN(d)) {
return d;
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
} }
} }