NumberFormat 似乎并不关心 BigDecimal 的
scale
,因此给定的尾随零会丢失:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
nf.setMaximumFractionDigits(Integer.MAX_VALUE);
BigDecimal a = new BigDecimal("1234.56789"); //scale=5
BigDecimal b = new BigDecimal("1234.567890"); //scale=6
a.toString(); //1234.56789
b.toString(); //1234.567890 <-- ✅ decimal place is preserved
nf.format(a); //1.234,56789
nf.format(b); //1.234,56789 <-- ❌ decimal place is lost
除非有更好的方法:
synchronized (nf) {
nf.setMinimumFractionDigits(number.scale());
nf.format(number); //1.234,567890 <-- ✅ decimal place is preserved
}