1.利用Math.round()的方法:
兩個int型的數相除,結果保留小數點后兩位:
int a=1188;
int b=93;
double c;
c=(double)(Math.round(a/b)/100.0);//這樣為保持2位
打印結果:c=0.12
c=new Double(Math.round(a/b)/1000.0);//這樣為保持3位
打印結果:c=0.012
2.另一種辦法
import java.text.DecimalFormat;
DecimalFormat df2 = new DecimalFormat("###.00");//這樣為保持2位
DecimalFormat df2 = new DecimalFormat("###.000");//這樣為保持3位
System.out.println(df2.format(double類型的變量));
PS:
Math.round()的作用:
double a=123.55
System.out.println(Math.round(a));
打印結果:124