import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
double number = 3.1415926;
DecimalFormat df = new DecimalFormat("#.0#");
String formatted;
if (number % 1 == 0) {
formatted = df.format(number);
} else {
df.setMaximumFractionDigits(2);
formatted = df.format(number);
}
System.out.println(formatted); // 输出:3.14
}
}
标签:java,df,浮点数,formatted,number,位数,DecimalFormat
From: https://www.cnblogs.com/chenyi502/p/17739983.html