格式化小数点和百分号 DecimalFormatter

1.

public class DecimalFormatterTest {
    public static void main(String[] args) {
      //DecimalFormat df = new DecimalFormat("0.00#"); //两个构造器
        DecimalFormat decimalFormat = new DecimalFormat();

        decimalFormat.applyPattern(".###");
        System.out.println(decimalFormat.format(23.112));//23.112
        System.out.println(decimalFormat.format(0.112));//.112

        decimalFormat.applyPattern("0.00#");//推荐
        System.out.println(decimalFormat.format(23.112));//23.112
        System.out.println(decimalFormat.format(0.112));//0.112

        decimalFormat.applyPattern("0.00%");//推荐
        System.out.println(decimalFormat.format(23.112));//2311.20%
        System.out.println(decimalFormat.format(0.112));//11.20%
        System.out.println(decimalFormat.format(0.2345));//23.45%

        decimalFormat.applyPattern(".##%");
        System.out.println(decimalFormat.format(23.112));//2311.2%
        System.out.println(decimalFormat.format(0.112));//11.2%
        System.out.println(decimalFormat.format(0.2345));//23.45%

    }
}

 

posted @ 2016-09-12 17:37  黑土白云  阅读(2105)  评论(0编辑  收藏  举报