查看DecimalFormat課程。我想你想要的是類似於
DecimalFormat df = new DecimalFormat();
// By default, there will a locale specific thousands grouping.
// Remove the statement if you want thousands grouping.
// That is, for a number 12345, it is printed as 12,345 on my machine
// if I remove the following line.
df.setGroupingUsed(false);
// default is 3. Set whatever you think is good enough for you. 340 is max possible.
df.setMaximumFractionDigits(340);
df.setDecimalSeparatorAlwaysShown(false);
BigDecimal bd = new BigDecimal("1234.5678900000");
System.out.println(df.format(bd));
bd = new BigDecimal("1234.00");
System.out.println(df.format(bd));
Output:
1234.56789
1234
您還可以使用您選擇的RoundingMode。通過使用提供給DecimalFormat構造函數的模式控制要顯示的小數點數。有關更多格式的詳細信息,請參閱DecimalFormat文檔。
我不確定這是他/她正在尋找的行爲。我認爲他們正在尋找剝離所有尾隨零。 – Zong
更新了答案。感謝您的評論。我錯讀了這個問題。 –