string To Double - Android java.lang

Android examples for java.lang:Double

Description

string To Double

Demo Code


import java.math.RoundingMode;
import java.text.NumberFormat;
import android.util.Log;

public class Main{
    /*from  w ww. j a  v a 2s .com*/
    public static double stringToDouble(String numStr, int digits) {

        NumberFormat df = NumberFormat.getNumberInstance();
        df.setGroupingUsed(false); // 
        df.setMaximumFractionDigits(digits); // 
        df.setRoundingMode(RoundingMode.HALF_UP); // ?

        if (StringUtils.isEmpty(numStr)) //  0.xxx
            return Double.valueOf(df.format(0));

        double result;
        try {
            result = Double.valueOf(numStr);
        } catch (NumberFormatException e) { // 1.2.3?
            e.printStackTrace();

            result = 0.0;
        }

        result = Double.valueOf(df.format(result));

        Log.d("result", numStr + " --> " + result);

        return result;
    }
}

Related Tutorials