/** * Checks if the given string is a float string that can be converted to a * double value. * * @param sStr * The string to check. May be <code>null</code>. * @return <code>true</code> if the value can be converted to a valid value */ public static boolean isFloat (@Nullable final String sStr) { return !Float.isNaN (parseFloat (sStr, Float.NaN)); } }
/** * Parse the given {@link Object} as float. Note: both the locale independent * form of a float can be parsed here (e.g. 4.523) as well as a localized form * using the comma as the decimal separator (e.g. the German 4,523). * * @param aObject * The object to parse. May be <code>null</code>. * @param fDefault * The default value to be returned if the passed object could not be * converted to a valid value. * @return The default value if the object does not represent a valid value. */ public static float parseFloat (@Nullable final Object aObject, final float fDefault) { if (aObject == null) return fDefault; if (aObject instanceof Number) return ((Number) aObject).floatValue (); return parseFloat (aObject.toString (), fDefault); }
/** * Parse the given {@link Object} as {@link Float}. Note: both the locale * independent form of a double can be parsed here (e.g. 4.523) as well as a * localized form using the comma as the decimal separator (e.g. the German * 4,523). * * @param aObject * The object to parse. May be <code>null</code>. * @param aDefault * The default value to be returned if the parsed object cannot be * converted to a float. May be <code>null</code>. * @return <code>aDefault</code> if the object does not represent a valid * value. */ @Nullable public static Float parseFloatObj (@Nullable final Object aObject, @Nullable final Float aDefault) { final float fValue = parseFloat (aObject, Float.NaN); return Float.isNaN (fValue) ? aDefault : Float.valueOf (fValue); }
/** * Parse the given {@link String} as {@link Float}. Note: both the locale * independent form of a double can be parsed here (e.g. 4.523) as well as a * localized form using the comma as the decimal separator (e.g. the German * 4,523). * * @param sStr * The string to parse. May be <code>null</code>. * @param aDefault * The default value to be returned if the parsed string cannot be * converted to a float. May be <code>null</code>. * @return <code>aDefault</code> if the object does not represent a valid * value. */ @Nullable public static Float parseFloatObj (@Nullable final String sStr, @Nullable final Float aDefault) { final float fValue = parseFloat (sStr, Float.NaN); return Float.isNaN (fValue) ? aDefault : Float.valueOf (fValue); }