silence-x

转载:https://blog.csdn.net/xue_feitian/article/details/6556275

第一种方法:

1 double f = 123.2315455458;
2 BigDecimal b = new BigDecimal(f);
3 double f1 = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();    //保留3位小数,最后一位四舍五入
4 System.out.println(f1);

输出为:123.232

第二种方法:

1 DecimalFormat df = new DecimalFormat("#.000");  //#.000保留三位小数,依次类推
2 String f = df.format(123.2315452);         //四舍五入
3 System.out.println(f);

输出为:123.232

第三种方法:

1 double d = 123.2315455458;
2 String s = String.format("%.6f", d);    //保留6位小数,最后一位四舍五入
3 System.out.println(s);

输出为:123.231546

%. 表示小数点前任意位数   6 表示两位小数 格式后的结果为 f 表示浮点型

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-27
  • 2021-09-05
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2022-12-23
猜你喜欢
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-29
  • 2021-06-08
  • 2022-12-23
相关资源
相似解决方案