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:Main.java

public static float readFloatAttribute(XmlPullParser in, String name) throws IOException {
    final String value = in.getAttributeValue(null, name);
    try {/*  w w  w  .  j a va 2 s.  c o m*/
        return Float.parseFloat(value);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing " + name + "=" + value + " as long");
    }
}

From source file:Main.java

public static void writeSharedPreferences(Context context, String prefsName, String name, Object value) {
    if (name == null || value == null) {
        return;/* w ww.  j  av  a2 s.  c om*/
    }
    SharedPreferences user = context.getSharedPreferences(prefsName, 0);
    Editor editor = user.edit();
    if (value instanceof Integer) {
        editor.putInt(name, Integer.parseInt(value.toString()));
    } else if (value instanceof Long) {
        editor.putLong(name, Long.parseLong(value.toString()));
    } else if (value instanceof Boolean) {
        editor.putBoolean(name, Boolean.parseBoolean(value.toString()));
    } else if (value instanceof String) {
        editor.putString(name, value.toString());
    } else if (value instanceof Float) {
        editor.putFloat(name, Float.parseFloat(value.toString()));
    }
    editor.commit();
}

From source file:Main.java

/**
 * Searches the given string for the first floating point number it contains,
 * parses and returns it./*from  w w  w . jav  a 2  s  . co  m*/
 */
public synchronized static float findFloat(String val) {
    if (val == null)
        return 0f;

    fpMatch.reset(val);
    if (!fpMatch.find())
        return 0f;

    val = fpMatch.group(1);
    //System.err.println("Parsing " + val);

    float retVal = 0f;
    try {
        retVal = Float.parseFloat(val);
        String units = fpMatch.group(6);
        if ("%".equals(units))
            retVal /= 100;
    } catch (Exception e) {
    }
    return retVal;
}

From source file:com.liferay.maven.plugins.util.GetterUtil.java

public static float getFloat(String value, float defaultValue) {
    if (value == null) {
        return defaultValue;
    }/*from  w  w  w  .ja  v a2s . c  o  m*/

    try {
        return Float.parseFloat(value.trim());
    } catch (Exception e) {
    }

    return defaultValue;
}

From source file:Main.java

/**
 * Get a float from the value received//from   w w  w  .  ja  v  a2 s  .c o m
 * @param value Object to convert
 * @return int converted
 */
public static float getFloat(Object value) {
    if (value instanceof Integer) {
        return ((Integer) value).floatValue();
    } else if (value instanceof Double) {
        return ((Double) value).floatValue();
    } else if (value instanceof Float) {
        return ((Float) value).floatValue();
    } else {
        try {
            return Float.parseFloat(value.toString());
        } catch (NumberFormatException nfe) {
            return 0;
        }
    }
}

From source file:Main.java

/**
 * Parses a percentage and returns a scaled float.
 * @param s contains the number to parse.
 * @return a float scaled number. 1.0 represents 100%.
 * @throws NumberFormatException if the number format is invalid or does not end with '%'.
 *//*  w  ww .  j av  a  2s.  c  o  m*/
public static float parsePercentage(String s) throws NumberFormatException {
    if (!s.endsWith("%")) {
        throw new NumberFormatException("Percentages must end with %");
    }
    return Float.parseFloat(s.substring(0, s.length() - 1)) / 100;
}

From source file:Main.java

public static float parse(String str, float fallback) {
    try {/*from www  .jav a2 s.  c  om*/
        return Float.parseFloat(str);
    } catch (NumberFormatException e) {
        return fallback;
    }
}

From source file:Main.java

public static float[][] readMatrix(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {//from   w  w w  . jav  a2 s. c  om
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    float[][] mat = new float[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Float val = Float.parseFloat(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Main.java

public static float getAndroidVersion() {
    if (sAndroidVersion == 0.0f) {
        String androidVersion = Build.VERSION.RELEASE.trim();
        try {/*from  w  ww  .  java 2  s.c o m*/
            int firstDotPosition = androidVersion.indexOf('.'),
                    secondDotPosition = androidVersion.indexOf('.', firstDotPosition + 1);
            sAndroidVersion = Float.parseFloat(androidVersion.substring(0,
                    secondDotPosition != -1 && secondDotPosition < 6 ? secondDotPosition : firstDotPosition));
        } catch (Throwable e) {
            sAndroidVersion = Float.parseFloat(androidVersion.substring(0, 1));
        }
    }
    return sAndroidVersion;
}

From source file:Main.java

/**
 * Returns the value for key in dictionary as a float or the given default
 * value if no value is defined for the key.
 * //from www. ja v  a  2 s . co m
 * @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 value for key in dict.
 */
public static float getFloat(Map<String, Object> dict, String key, float defaultValue) {
    Object value = dict.get(key);

    if (value == null) {
        return defaultValue;
    } else {
        return Float.parseFloat(value.toString());
    }
}