Java两整数相除保留两位小数

  1. int num1 = 7;
  2. int num2 = 9;
  3. // 创建一个数值格式化对象
  4. NumberFormat numberFormat = NumberFormat.getInstance();
  5. // 设置精确到小数点后2位
  6. numberFormat.setMaximumFractionDigits(2);
  7. String result = numberFormat.format((float) num1 / (float) num2 * 100);
  8. System.out.println("num1和num2的百分比为:" + result + "%");
posted @ 2019-10-17 15:01  桔子木木  阅读(7995)  评论(0编辑  收藏  举报