我正在使用以下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");
}
};
这将执行以下转换:
1 -> 1.00
1.1 -> 1.10
1.12 -> 1.12
我现在有一个额外的要求。
1.123 -> 1.123
1.1234 -> 1.123
这意味着当
- 少于两位小数,我将“填充”到两位小数。
- 正好有两位或三位小数,我什么也不做。
- 小数点后三位以上,我将截断到小数点后三位。
我可以在DecimalFormat
课堂上指定这种行为吗?