Java Float Number Create toFloat(String value, float defaultValue)

Here you can find the source of toFloat(String value, float defaultValue)

Description

convert the string to an float, and return the default value if the string is null or does not contain a valid int value

License

Open Source License

Parameter

Parameter Description
value string value
defaultValue default value

Return

float

Declaration

static public float toFloat(String value, float defaultValue) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  w  ww.java 2 s  .  c om*/
     * convert the string to an float, and return the default value if
     * the string is null or does not contain a valid int value
     *
     * @param value string value
     * @param defaultValue default value
     *
     * @return float
     */
    static public float toFloat(String value, float defaultValue) {
        if (value != null) {
            try {
                return Float.parseFloat(value);
            } catch (NumberFormatException n) {
            }
        }
        return defaultValue;
    }

    /**
     * convert the string to an float, and return 0 if
     * the string is null or does not contain a valid int value
     *
     * @param value string value
     *
     * @return float
     */
    static public float toFloat(String value) {
        if (value != null) {
            try {
                return Float.parseFloat(value);
            } catch (NumberFormatException n) {
            }
        }
        return 0;
    }
}

Related

  1. toFloat(String text)
  2. toFloat(String value)
  3. toFloat(String value)
  4. toFloat(String value, float _default)
  5. toFloat(String value, Float defaultValue)
  6. toFloat(T value)
  7. toFloatA(byte[] data)
  8. toFloatObject(Object o)