TF-EssentialsX/Essentials/src/com/earth2me/essentials/utils/FloatUtil.java

31 lines
917 B
Java
Raw Normal View History

package com.earth2me.essentials.utils;
/**
* parseFloat and parseDouble proxies that are protected against non-finite values.
*/
2015-06-05 15:38:06 +00:00
public class FloatUtil {
private FloatUtil() {
}
2015-06-05 15:38:06 +00:00
public static float parseFloat(String s) throws NumberFormatException {
float f = Float.parseFloat(s);
2015-06-05 15:38:06 +00:00
if (Float.isNaN(f)) {
throw new NumberFormatException("NaN is not valid");
}
2015-06-05 15:38:06 +00:00
if (Float.isInfinite(f)) {
throw new NumberFormatException("Infinity is not valid");
}
return f;
}
2015-06-05 15:38:06 +00:00
public static double parseDouble(String s) throws NumberFormatException {
double d = Double.parseDouble(s);
2015-06-05 15:38:06 +00:00
if (Double.isNaN(d)) {
throw new NumberFormatException("NaN is not valid");
}
2015-06-05 15:38:06 +00:00
if (Double.isInfinite(d)) {
throw new NumberFormatException("Infinity is not valid");
}
return d;
}
}