我有一个像32.559
这样的双倍。
我如何除掉一个十进制数字以外的所有数字,以便我的输出看起来像32.5
?
答案 0 :(得分:0)
float f = 32.559f;
System.out.printf("%.1f", f);
答案 1 :(得分:0)
我假设您只想以这种方式打印:
String result = String.format("%.1f", value);
答案 2 :(得分:0)
您可以使用DecimalFormat
:
Locale locale = new Locale("en", "UK");
String pattern = "###.#";
DecimalFormat decimalFormat = (DecimalFormat)
NumberFormat.getNumberInstance(locale);
decimalFormat.applyPattern(pattern);
String format = decimalFormat.format(32.559);
System.out.println(format);
此代码打印的输出为:
32.5
这是上面的基本版,你可以抛出here了解更多细节。