大家好,又见面了,我是你们的朋友全栈君。
double x = 8.055;
System.out.printf("%.2f\n",x);//8.06
double x = 8.055;
System.out.format("%.2f\n",x);//8.06
double x = 8.055;
String s = String.format("%.2f", x);
System.out.println( s );
import java.util.Formatter;//不要忘了在类的外面导入这个包
Formatter a = new Formatter(System.out);
double x = 8.055;
a.format("%.2f\n", x);//8.06
a.close();//关闭a
import java.text.DecimalFormat;//不要忘记导入包
DecimalFormat df = new DecimalFormat(".00");
double x = 8.055;
System.out.println(df.format(x));//8.05
import java.text.NumberFormat;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);//不足两位不补0,超过两位部分的自动舍去
double x = 8.055;
double y = 8.5;
System.out.println(nf.format(x));//8.05
System.out.println(nf.format(y));//8.5
import java.text.NumberFormat;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(2);//不足两位自动补0,超过两位的部分不舍去
double x = 8.055;
double y = 8.5;
System.out.println(nf.format(x));//8.055
System.out.println(nf.format(y));//8.50
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/157146.html原文链接:https://javaforall.cn