使用DecimalFormat显示填充零

mpo*_*z08 33 java formatting decimalformat

我正在使用DecimalFormat将双精度格式化为2位小数,如下所示:

DecimalFormat dec = new DecimalFormat("#.##");
double rawPercent = ( (double)(count.getCount().intValue()) / 
                            (double)(total.intValue()) ) * 100.00;
double percentage = Double.valueOf(dec.format(rawPercent));
Run Code Online (Sandbox Code Playgroud)

它工作,但如果我有一个像20这样的数字,它给了我这个:

20.0
Run Code Online (Sandbox Code Playgroud)

我想要这个:

20.00
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Dou*_*ter 35

DecimalFormat类用于将十进制数值转换为String.在您的示例中,您将获取来自format()方法的String并将其放回到double变量中.如果您正在输出该双变量,则不会看到格式化的字符串.请参阅下面的代码示例及其输出:

int count = 10;
int total = 20;
DecimalFormat dec = new DecimalFormat("#.00");
double rawPercent = ( (double)(count) / (double)(total) ) * 100.00;

double percentage = Double.valueOf(dec.format(rawPercent));

System.out.println("DF Version: " + dec.format(rawPercent));
System.out.println("double version: " + percentage);
Run Code Online (Sandbox Code Playgroud)

哪个输出:

"DF Version: 50.00"
"double version: 50.0"
Run Code Online (Sandbox Code Playgroud)


小智 14

试试这段代码:

BigDecimal decimal = new BigDecimal("100.25");

BigDecimal decimal2 = new BigDecimal("1000.70");

BigDecimal decimal3 = new BigDecimal("10000.00");

DecimalFormat format = new DecimalFormat("###,###,###,###,###.##");

format.setDecimalSeparatorAlwaysShown(true);

format.setMinimumFractionDigits(2);

System.out.println(format.format(decimal));

System.out.println(format.format(decimal2));

System.out.println(format.format(decimal3));
Run Code Online (Sandbox Code Playgroud)

结果:

100.25

1,000.70

10,000.00
Run Code Online (Sandbox Code Playgroud)


coo*_*eek 8

您可以尝试以下方式:

DecimalFormat df = new DecimalFormat("0.000000");
df.setMinimumFractionDigits(0);
df.setMinimumIntegerDigits(2);
Run Code Online (Sandbox Code Playgroud)

这样,您可以确保小数点前后的最小位数


ZZ *_*der 7

使用格式"#.00".

  • 最好使用“ 0.00”作为“#.00”将0设置为.00,这在我看来并不好。 (2认同)

Dim*_*ele 6

我发现我的小测试程序很有用,并想与您分享。享受。

\n\n
package be.softwarelab.numbers;\n\nimport java.text.DecimalFormat;\nimport java.text.DecimalFormatSymbols;\nimport java.util.Locale;\n\npublic class DecimalNumbers {\n\n    private static final double ZERO = 0;\n    private static final double TEN = 10.0;\n    private static final double PI = Math.PI;                           // 3.141592653589793;\n    private static final double MILLIONS = Math.E * Math.pow(10, 6);    // 2718281.828459045;\n    private static final double NINERS = 9999999.99999;\n\n    public static void main(String[] args) {\n\n        String format01 = "#.#";\n        String format02 = "0.#";\n        String format03 = "#.0";\n        String format04 = "0.0";\n        String format05 = "##.#";\n        String format06 = "00.#";\n\n        String formatAll = "###,###.###";\n        String formatLong = "#.#########";  \n\n        System.out.println("====== ZERO =================================================");\n        showResult(ZERO, format01, Locale.US);\n        showResult(ZERO, format02, Locale.US);\n        showResult(ZERO, format03, Locale.US);\n        showResult(ZERO, format04, Locale.US);\n        showResult(ZERO, format05, Locale.US);\n        showResult(ZERO, format06, Locale.US);\n        System.out.println("====== TEN  =================================================");\n        showResult(TEN, format01, Locale.US);\n        showResult(TEN, format02, Locale.US);\n        showResult(TEN, format03, Locale.US);\n        showResult(TEN, format04, Locale.US);\n        showResult(TEN, format05, Locale.US);\n        showResult(TEN, format06, Locale.US);\n        System.out.println("====== PI   =================================================");\n        showResult(PI, format01, Locale.US);\n        showResult(PI, format02, Locale.US);\n        showResult(PI, format03, Locale.US);\n        showResult(PI, format04, Locale.US);\n        showResult(PI, format05, Locale.US);\n        showResult(PI, format06, Locale.US);\n        System.out.println("====== MILLIONS =============================================");\n        showResult(MILLIONS, formatAll, Locale.US);\n        showResult(MILLIONS, formatAll, Locale.GERMANY);\n        showResult(MILLIONS, formatAll, Locale.FRANCE);\n        showResult(MILLIONS, formatAll, new Locale("nl", "BE"));\n        System.out.println("====== NINERS   =============================================");\n        showResult(NINERS, format01, Locale.US);\n        showResult(NINERS, format02, Locale.US);\n        showResult(NINERS, format03, Locale.US);\n        showResult(NINERS, format04, Locale.US);\n        showResult(NINERS, format05, Locale.US);\n        showResult(NINERS, format06, Locale.US);\n        showResult(NINERS, formatLong, Locale.US);\n        System.out.println("=============================================================");\n    }\n\n    public static void showResult(double number, String format, Locale locale) {\n        // Using a Locale to see the differences between regions.\n        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(locale);\n        DecimalFormat formatter = new DecimalFormat (format, otherSymbols);\n\n        // Create the String result\n        String output = formatter.format(number);\n\n        // Format the output for a nice presentation.\n        System.out.format("    %s %20s %11s = %20s\\n", locale, number, format, output);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这导致:

\n\n
====== ZERO =================================================\nen_US                  0.0         #.# =                    0\nen_US                  0.0         0.# =                    0\nen_US                  0.0         #.0 =                   .0\nen_US                  0.0         0.0 =                  0.0\nen_US                  0.0        ##.# =                    0\nen_US                  0.0        00.# =                   00\n====== TEN  =================================================\nen_US                 10.0         #.# =                   10\nen_US                 10.0         0.# =                   10\nen_US                 10.0         #.0 =                 10.0\nen_US                 10.0         0.0 =                 10.0\nen_US                 10.0        ##.# =                   10\nen_US                 10.0        00.# =                   10\n====== PI   =================================================\nen_US    3.141592653589793         #.# =                  3.1\nen_US    3.141592653589793         0.# =                  3.1\nen_US    3.141592653589793         #.0 =                  3.1\nen_US    3.141592653589793         0.0 =                  3.1\nen_US    3.141592653589793        ##.# =                  3.1\nen_US    3.141592653589793        00.# =                 03.1\n====== MILLIONS =============================================\nen_US    2718281.828459045 ###,###.### =        2,718,281.828\nde_DE    2718281.828459045 ###,###.### =        2.718.281,828\nfr_FR    2718281.828459045 ###,###.### =        2\xc2\xa0718\xc2\xa0281,828\nnl_BE    2718281.828459045 ###,###.### =        2.718.281,828\n====== NINERS   =============================================\nen_US        9999999.99999         #.# =             10000000\nen_US        9999999.99999         0.# =             10000000\nen_US        9999999.99999         #.0 =           10000000.0\nen_US        9999999.99999         0.0 =           10000000.0\nen_US        9999999.99999        ##.# =             10000000\nen_US        9999999.99999        00.# =             10000000\nen_US        9999999.99999 #.######### =        9999999.99999\n=============================================================\n
Run Code Online (Sandbox Code Playgroud)\n


Vin*_*nay 5

试试这个代码:

int count = 10;
int total = 20;
int another=0;
DecimalFormat df = new DecimalFormat("0.00");

System.out.println(df.format(count));
System.out.println(df.format(total ));
System.out.println(df.format(another));
Run Code Online (Sandbox Code Playgroud)

输出为:10.00 20.00 0.00