Example usage for java.lang Float parseFloat

List of usage examples for java.lang Float parseFloat

Introduction

In this page you can find the example usage for java.lang Float parseFloat.

Prototype

public static float parseFloat(String s) throws NumberFormatException 

Source Link

Document

Returns a new float initialized to the value represented by the specified String , as performed by the valueOf method of class Float .

Usage

From source file:Utils.java

public static float getElementFloatValue(Document document, Element parent, String element) {
    return Float.parseFloat(getElementStringValue(document, parent, element));
}

From source file:Main.java

public static float getFloatAttribute(@NonNull Context context, @NonNull AttributeSet attrs,
        @NonNull String name, float defaultValue) {
    String number = getAttribute(context, attrs, name, null);
    if (number != null)
        return Float.parseFloat(number);
    else// w  ww.j  a va  2 s .co m
        return defaultValue;
}

From source file:Main.java

/**
 * Returns the value for key in dictionary as an int or the given default
 * value if no value is defined for the key.
 * /*from   ww  w.j a v a 2s  . c om*/
 * @param dict
 *            Dictionary that contains the key, value pairs.
 * @param key
 *            Key whose value should be returned.
 * @param defaultValue
 *            Default value to return if the key is undefined.
 * @return Returns the integer value for key in dict.
 */
public static int getInt(Map<String, Object> dict, String key, int defaultValue) {
    Object value = dict.get(key);

    if (value == null) {
        return defaultValue;
    } else {
        // Handles commas by casting them to an int
        return (int) Float.parseFloat(value.toString());
    }
}

From source file:Main.java

/**
 * Convert a String to a float if possible. Instead of throwing an exception like Float.parseFloat() does, return a specified default value if an error is
 * encountered/*from  ww w.  j a va  2s.  com*/
 * 
 * @param string
 *            String to be parsed
 * @param defaultValue
 *            default value to be returned if a parse error
 * @return parsed float value
 */
public static float parseFloat(final String string, final float defaultValue) {
    try {
        return Float.parseFloat(string);
    } catch (final Exception e) {
        return defaultValue;
    }
}

From source file:Main.java

private static int parseSingleChanel(String chanel) {
    if (chanel.contains("%")) {
        float percent = Float.parseFloat(chanel.replaceAll("[^0-9\\.]", ""));
        return (int) (255 * (percent / 100));
    }//from  w w  w  .  j  ava  2s .  c o  m
    return Integer.parseInt(chanel.replaceAll("[^0-9]", ""));
}

From source file:Main.java

/**
 * Parses the supplied xsd:float string and returns its value.
 * // w ww. j a va 2 s .co m
 * @param s
 *        A string representation of an xsd:float value.
 * @return The <tt>float</tt> value represented by the supplied string argument.
 * @throws NumberFormatException
 *         If the supplied string is not a valid xsd:float value.
 */
public static float parseFloat(String s) {
    if (POSITIVE_INFINITY.equals(s)) {
        return Float.POSITIVE_INFINITY;
    } else if (NEGATIVE_INFINITY.equals(s)) {
        return Float.NEGATIVE_INFINITY;
    } else if (NaN.equals(s)) {
        return Float.NaN;
    } else {
        s = trimPlusSign(s);
        return Float.parseFloat(s);
    }
}

From source file:ConsoleUtils.java

public static float readFloat() {
    return Float.parseFloat(readData());
}

From source file:blue.components.lines.LinePoint.java

public static LinePoint loadFromXML(Element data) {
    LinePoint lp = new LinePoint();

    float x = Float.parseFloat(data.getAttributeValue("x"));
    float y = Float.parseFloat(data.getAttributeValue("y"));

    lp.setLocation(x, y);//from  ww  w.  j a v  a2  s .  com

    return lp;
}

From source file:Main.java

/**
 * Returns the value for key in dictionary as a float array or the given default
 * value if no value is defined for the key.
 * /*from w  w w  . j a  v  a2 s. c om*/
 * @param dict
 *            Dictionary that contains the key, value pairs.
 * @param key
 *            Key whose value should be returned.
 * @param defaultValue
 *            Default value to return if the key is undefined.
 * @return Returns the float array value for key in dict.
 */
public static float[] getFloatArray(Map<String, Object> dict, String key, float[] defaultValue,
        String separator) {
    Object value = dict.get(key);

    if (value == null) {
        return defaultValue;
    } else {
        String[] floatChars = value.toString().split(separator);
        float[] result = new float[floatChars.length];

        for (int i = 0; i < floatChars.length; i++) {
            result[i] = Float.parseFloat(floatChars[i]);
        }

        return result;
    }
}

From source file:JvmVersionUtil.java

/**
 * Gets the version of the JVM.//from   w  ww. jav a 2  s.  c o  m
 * 
 * @return The integer that corresponds with the version of the JVM. This
 *         can be VERSION_3_OR_LESS,VERSION_4_or_5, VERSION_6_OR_MORE.
 * @throws IllegalStateException
 *             If the system property <code>java.version</code> does not
 *             start with a x.x. x should be a number.
 */
public static int getVersion() {

    // Get the system property java.vm.version.
    String jVersion = System.getProperty("java.version");

    // Get the version number.
    int pointPosition = jVersion.indexOf(".");
    if (pointPosition > 0) {
        if (jVersion.length() >= pointPosition + 2) {
            String version = jVersion.substring(0, pointPosition + 2);

            try {
                float versionNumber = Float.parseFloat(version);
                if (versionNumber < VERSION_4) {
                    return VERSION_3_OR_LESS;
                }
                if (versionNumber < VERSION_6) {
                    return VERSION_4_or_5;
                } else {
                    return VERSION_6_OR_MORE;
                }
            } catch (NumberFormatException exception) {
                throw new IllegalStateException("Unknown java version [" + jVersion + "].");
            }
        }
    }

    throw new IllegalStateException("Unknown java version [" + jVersion + "].");

}