将money用逗号隔开

import java.text.DecimalFormat;

/**
 * Created by Nicole on 6/2/2014.
 */
public class NicoleFormatUtils {

    /**
     * 将金额分别三位数三位数的用逗号隔开
     * @param text 金额String
     * @return String
     */
    private static  String fmtMicrometer(String text) {
        DecimalFormat df = null;
        if (text.indexOf(".") > 0) {
            if (text.length() - text.indexOf(".")-1 == 0) {
                df = new DecimalFormat("###,##0.");
            }else if (text.length() - text.indexOf(".")-1 == 1) {
                df = new DecimalFormat("###,##0.0");
            }else {
                df = new DecimalFormat("###,##0.00");
            }
        }else {
            df = new DecimalFormat("###,##0");
        }
        double number = 0.00;
        try {
            number = Double.parseDouble(text);
        } catch (Exception e) {
            number = 0.0;
        }
        return  df.format(number);
    }

    public static void main(String[] args) {
        String str = "354364675788";
        System.out.println(fmtMicrometer(str));
    }

}



你可能感兴趣的:(将money用逗号隔开)