Java中数字处理去掉末尾的0

public static String insertComma(String s, int len) {
    if (s == null || s.length() < 1) {
        return "";
    }
    NumberFormat formater = null;
    double num = Double.parseDouble(s);
    if (len == 0) {
        formater = new DecimalFormat("###,###");
 
    } else {
        StringBuffer buff = new StringBuffer();
        buff.append("###,###.");
        for (int i = 0; i < len; i++) {
            buff.append("#");
        }
        formater = new DecimalFormat(buff.toString());
    }
    return formater.format(num);
}


double num = 5.5500;

DecimalFormat decimalFormat = new DecimalFormat("##########.##########");

String numConverted = decimalFormat.format(num); //5.55



利用“########.##########”

你可能感兴趣的:(Java,Java,string)