如何根据输入长度更改DecimalFormat行为?

Che*_*eng 8 java format decimal number-formatting decimalformat

我使用以下DecimalFormat模式:

// Use ThreadLocal to ensure thread safety.
private static final ThreadLocal <NumberFormat> numberFormat =
  new ThreadLocal <NumberFormat>() {
    @Override protected NumberFormat initialValue() {
        return new DecimalFormat("#,##0.00");
    }
};
Run Code Online (Sandbox Code Playgroud)

这将执行以下转换:

1    -> 1.00
1.1  -> 1.10
1.12 -> 1.12
Run Code Online (Sandbox Code Playgroud)

我现在有一个额外的要求.

1.123  -> 1.123
1.1234 -> 1.123
Run Code Online (Sandbox Code Playgroud)

这意味着什么时候

  • 小数位少于两位,我将"填充"到小数点后两位.
  • 正好有两三个小数位,我什么都不做.
  • 有三个以上的小数位,我将截断到三位小数.

我可以在DecimalFormat课堂上指定这种行为吗?