我有一些 double ,我用它们格式化了
Locale loc = new Locale("hu", "HU");
NumberFormat format = NumberFormat.getInstance(loc);
现在我的 double 看起来像这样:1 432,321,我怎样才能将它们格式化为这样:1 432,3,有什么想法吗?
最佳答案
您可以在 NumberFormat
上使用 setMaximumFractionDigits
方法。
Locale loc = new Locale("hu", "HU");
NumberFormat format = NumberFormat.getInstance(loc);
format.setMaximumFractionDigits(1);
那么你的输出是“1 432,3”
这会将输入的任何内容四舍五入为正确的位数。例如,2.35 将被格式化为“2,4”
关于Java 数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33737107/