其他分享
首页 > 其他分享> > DecimalFormat 数字格式化

DecimalFormat 数字格式化

作者:互联网

 

字符串数字的格式

import java.text.*;

class DecimalFormatDemo {
    static public void main(String[] args) {
        customFormat("###,###.###", 123456.789);
        customFormat("###.##", 123456.789);
        customFormat("000000.000", 123.78);
        customFormat("$###,###.###", 12345.67);
    }

    /**
     * Prints a double value formatted according to a given pattern.
     */
    static public void customFormat(String pattern, double value) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        String output = myFormatter.format(value);
        System.out.println(value + "  " + pattern + "  " + output);
    }
}
123456.789  ###,###.###  123,456.789
123456.789  ###.##  123456.79
123.78  000000.000  000123.780
12345.67  $###,###.###  $12,345.67
Output:

 

 

标签:customFormat,格式化,数字,pattern,value,###.###,123456.789,DecimalFormat,###
来源: https://www.cnblogs.com/WLCYSYS/p/15250290.html