Java数字格式

Java数字格式,java,numbers,format,number-formatting,Java,Numbers,Format,Number Formatting,我有一些双精度的,我用 Locale loc = new Locale("hu", "HU"); NumberFormat format = NumberFormat.getInstance(loc); 现在我的替身看起来是这样的:1432321,我如何将它们格式化成这样:1432,3,你知道吗?你可以使用NumberFormat上的setMaximumFractionDigits方法 Locale loc = new Locale("hu", "HU"); NumberFormat for

我有一些双精度的,我用

Locale loc = new Locale("hu", "HU");
NumberFormat format = NumberFormat.getInstance(loc); 

现在我的替身看起来是这样的:1432321,我如何将它们格式化成这样:1432,3,你知道吗?

你可以使用
NumberFormat
上的
setMaximumFractionDigits
方法

Locale loc = new Locale("hu", "HU");
NumberFormat format = NumberFormat.getInstance(loc);
format.setMaximumFractionDigits(1);
那么您的输出是“1432,3”


这将把输入的任何内容四舍五入到正确的位数。例如,2.35将被格式化为“2,4”

这是格式化还是舍入?只是格式化,但如果可能的话舍入会更好