格式为double,左侧和右侧为零

Ste*_*bbi 2 java double string-formatting

我想根据需要在左右两边用零格式化双精度.

double myVal = 12.456;
Run Code Online (Sandbox Code Playgroud)

想要字符串 "012.456000"

我试过了:

String.format("%03.06f", myVal);
Run Code Online (Sandbox Code Playgroud)

和其他变化,但它似乎没有格式化小数点左侧的文本.

如果可能的话,我宁愿不为此引入第三方库.

Roh*_*ain 5

用途java.text.DecimalFormat:

double myVal = 12.456; 
DecimalFormat formatter = new DecimalFormat("000.000000");      
System.out.println(formatter.format(myVal));
Run Code Online (Sandbox Code Playgroud)