safe Parse Float - Android java.lang

Android examples for java.lang:Float

Description

safe Parse Float

Demo Code

import android.text.TextUtils;

public class Main{

    public static float safeParseFloat(String str, float fallback) {
        if (str == null) {
            // this check is needed for proper deserialization
            return fallback;
        }//from   w  w w  . java 2 s  . com

        try {
            return Float.valueOf(str);
        } catch (NumberFormatException e) {
            ULog.d(TAG, String.format("Cannot parse float from %s.", str));
            return fallback;
        }
    }

}

Related Tutorials