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

 

相关文章:

  • 2021-05-21
  • 2021-06-19
  • 2022-12-23
  • 2021-10-14
  • 2021-12-18
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-01-10
  • 2022-02-07
  • 2021-11-24
  • 2022-12-23
相关资源
相似解决方案