Java编程:数字格式化类 DecimalFormat

数字格式化场景:
1、进行金额数据展示时,保留小数点后两位。
2、进行重量数据展示时,如果小数点后为 0,则取整数。
3、生成数字编码时,要求 6 位数字,不够则以 0 补齐。
4、进行箱、盒、包的数据展示时,要求取整数。

代码如下:

package org.learn.format;

import java.text.DecimalFormat;

/**
 * @author zhibo
 * @version 1.0
 * @date 2018/12/7 17:11
 */
public class DecimalFormatDemo {


    public static void main(String[] args) {
        double doubleData = 3.1415927;
        System.out.println("==========取整数==========");
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###").format(doubleData));

        doubleData = 333.1415927;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###").format(doubleData));

        doubleData = 3333.1415927;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###").format(doubleData));

        System.out.println("==========取整数、小数==========");
        doubleData = 3.14;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000.000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###.###").format(doubleData));

        doubleData = 333.141;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000.000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###.###").format(doubleData));

        doubleData = 3333.1415927;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000.000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###.###").format(doubleData));

        System.out.println("==========小数点为后为零,则不显示小数点后==========");
        doubleData = 3333.00;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[000]:  " + new DecimalFormat("000.000").format(doubleData));
        System.out.println("[###]:  " + new DecimalFormat("###.###").format(doubleData));

        System.out.println("==========取百分数==========");
        doubleData = 0.8010324;
        System.out.println("doubleData=" + doubleData);
        System.out.println("[#.##%]:  " + new DecimalFormat("#.##%").format(doubleData));

        System.out.println("==========每3位加一个逗号==========");
        Long longData = 1101002000L;
        System.out.println("longData=" + longData);
        System.out.println("[,###]:  " + new DecimalFormat(",###").format(longData));
        System.out.println("[,000]:  " + new DecimalFormat(",000").format(longData));

        longData = 21L;
        System.out.println("longData=" + longData);
        System.out.println("[,###]:  " + new DecimalFormat(",###").format(longData));
        System.out.println("[,000]:  " + new DecimalFormat(",000").format(longData));

        System.out.println("==========科学技术法==========");
        longData = 1101002000L;
        System.out.println("longData=" + longData);
        System.out.println("[#.#####E0]:  " + new DecimalFormat("#.#####E0").format(longData));
        System.out.println("[#.#####E000]:  " + new DecimalFormat("#.#####E000").format(longData));
        System.out.println("[0.00000E0]:  " + new DecimalFormat("0.00000E0").format(longData));
        System.out.println("[0.00000E000]:  " + new DecimalFormat("0.00000E000").format(longData));
        /// E后面不可以加#号,会报异常
//        System.out.println("[#.#####E#]:  " + new DecimalFormat("#.#####E#").format(big));
//        System.out.println("[#.#####E###]:  " + new DecimalFormat("#.#####E###").format(big));

        longData = 21L;
        System.out.println("longData=" + longData);
        System.out.println("[#.#####E0]:  " + new DecimalFormat("#.#####E0").format(longData));
        System.out.println("[#.#####E000]:  " + new DecimalFormat("#.#####E000").format(longData));

        System.out.println("==========字符串替换==========");
        longData = 2000L;
        System.out.println("longData=" + longData);
        System.out.println("[000]:  " + new DecimalFormat("这座楼有000层").format(longData));
        System.out.println("[###]:  " + new DecimalFormat("这座楼有###层").format(longData));


        longData = 21L;
        System.out.println("longData=" + longData);
        System.out.println("[000]:  " + new DecimalFormat("这座楼有000层").format(longData));
        System.out.println("[###]:  " + new DecimalFormat("这座楼有###层").format(longData));
    }
}

你可能感兴趣的:(Java编程)