Format Decimal by decimal count - Android java.lang

Android examples for java.lang:Double

Description

Format Decimal by decimal count

Demo Code

import android.text.TextUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main{

    public static String getFormatDecimal(double data, int decimalNum) {
        String format = "";
        try {/*w  ww  . j a  va  2 s.  c o m*/
            StringBuilder fs = new StringBuilder();
            fs.append("#.");
            if (decimalNum < 1) {
                decimalNum = 3;
            }
            for (int i = 0; i < decimalNum; i++) {
                fs.append("#");
            }
            DecimalFormat df = new DecimalFormat(fs.toString());
            format = df.format(data);
        } catch (Exception e) {
            // TODO: handle exception
            e.toString();
        }
        return format;
    }

}

Related Tutorials