List of usage examples for java.lang Float parseFloat
public static float parseFloat(String s) throws NumberFormatException
From source file:Main.java
private static Object convertValType(Object value, Class fieldTypeClass) { Object retVal = null;/*from ww w .j a va2 s.co m*/ if (Long.class.getName().equals(fieldTypeClass.getName()) || long.class.getName().equals(fieldTypeClass.getName())) { retVal = Long.parseLong(value.toString()); } else if (Integer.class.getName().equals(fieldTypeClass.getName()) || int.class.getName().equals(fieldTypeClass.getName())) { retVal = Integer.parseInt(value.toString()); } else if (Float.class.getName().equals(fieldTypeClass.getName()) || float.class.getName().equals(fieldTypeClass.getName())) { retVal = Float.parseFloat(value.toString()); } else if (Double.class.getName().equals(fieldTypeClass.getName()) || double.class.getName().equals(fieldTypeClass.getName())) { retVal = Double.parseDouble(value.toString()); } else { retVal = value; } return retVal; }
From source file:Main.java
/** * Gets the node value as float.//from w ww .ja v a 2 s .c om * *@param node Description of the Parameter *@return The nodeValueAsFloat value *@exception DOMException Description of the Exception */ public final static float getNodeValueAsFloat(Node node) throws DOMException { if (node != null) { node = node.getFirstChild(); if (node != null) return Float.parseFloat(node.getNodeValue()); } return -1; }
From source file:Main.java
static public float getFloatTextContent(Element element) { String s = getTrimmedTextContent(element); return s == null || s.isEmpty() ? 0 : Float.parseFloat(s); }
From source file:Main.java
static public float getFloatAttribute(Element element, String name) { String s = getTrimmedAttribute(element, name); return s.isEmpty() ? 0f : Float.parseFloat(s); }
From source file:Main.java
public static float parseFloatSafely(String content, float defaultValue) { if (content == null) return defaultValue; try {/* w w w.j a v a 2 s .co m*/ return Float.parseFloat(content); } catch (NumberFormatException e) { return defaultValue; } }
From source file:Main.java
public static float getFloatNodeValue(Node node, String nodeName) { return Float.parseFloat(getStringNodeValue(node, nodeName)); }
From source file:Main.java
/** * /*from w w w .j a va 2 s.co m*/ * @Title: convertValType * @Description: TODO * @param value * @param fieldTypeClass * @return * @return: Object */ public static Object convertValType(Object value, Class<?> fieldTypeClass) { Object retVal = null; if (Long.class.getName().equals(fieldTypeClass.getName()) || long.class.getName().equals(fieldTypeClass.getName())) { retVal = Long.parseLong(value.toString()); } else if (Integer.class.getName().equals(fieldTypeClass.getName()) || int.class.getName().equals(fieldTypeClass.getName())) { retVal = Integer.parseInt(value.toString()); } else if (Float.class.getName().equals(fieldTypeClass.getName()) || float.class.getName().equals(fieldTypeClass.getName())) { retVal = Float.parseFloat(value.toString()); } else if (Double.class.getName().equals(fieldTypeClass.getName()) || double.class.getName().equals(fieldTypeClass.getName())) { retVal = Double.parseDouble(value.toString()); } else { retVal = value; } return retVal; }
From source file:Main.java
@Nullable public static Object valueFromString(String newValue, Object existingValue) throws IllegalArgumentException { if (existingValue instanceof Integer) { return Integer.parseInt(newValue); } else if (existingValue instanceof Long) { return Long.parseLong(newValue); } else if (existingValue instanceof Float) { return Float.parseFloat(newValue); } else if (existingValue instanceof Boolean) { return parseBoolean(newValue); } else if (existingValue instanceof String) { return newValue; } else if (existingValue instanceof Set) { try {/*from w w w . j av a 2 s . co m*/ JSONArray obj = new JSONArray(newValue); int objN = obj.length(); HashSet<String> set = new HashSet<String>(objN); for (int i = 0; i < objN; i++) { set.add(obj.getString(i)); } return set; } catch (JSONException e) { throw new IllegalArgumentException(e); } } else { throw new IllegalArgumentException("Unsupported type: " + existingValue.getClass().getName()); } }
From source file:Main.java
/** * This method parses the given value into the specified primitive or wrapper class. * @param clazz - primitive or wrapper class used to parse * @param value - string to be parsed// w w w .ja v a2 s. co m * @return object of type clazz parsed from the string * @author Trisan Bepler */ public static Object toObject(Class<?> clazz, String value) { if (Boolean.TYPE == clazz) return Boolean.parseBoolean(value); if (Byte.TYPE == clazz) return Byte.parseByte(value); if (Short.TYPE == clazz) return Short.parseShort(value); if (Integer.TYPE == clazz) return Integer.parseInt(value); if (Long.TYPE == clazz) return Long.parseLong(value); if (Float.TYPE == clazz) return Float.parseFloat(value); if (Double.TYPE == clazz) return Double.parseDouble(value); if (Boolean.class == clazz) return Boolean.parseBoolean(value); if (Byte.class == clazz) return Byte.parseByte(value); if (Short.class == clazz) return Short.parseShort(value); if (Integer.class == clazz) return Integer.parseInt(value); if (Long.class == clazz) return Long.parseLong(value); if (Float.class == clazz) return Float.parseFloat(value); if (Double.class == clazz) return Double.parseDouble(value); if (Character.class == clazz) return value.charAt(0); if (Character.TYPE == clazz) return value.charAt(0); return value; }
From source file:Main.java
public static boolean containsFloatArray(final Object src, final Object tgt, final Class<?> type) throws Exception { float value = Float.parseFloat((String) tgt); if (type.isPrimitive()) { float[] array = (float[]) src; for (float val : array) { if (val == value) return true; }/*w w w . j a v a2 s .c o m*/ } else { Float[] array = (Float[]) src; for (float val : array) { if (val == value) return true; } } return false; }