第一种

  1. import java.math.BigDecimal;
  2. /**
  3. * @author WGR
  4. * @create 2020/3/17 -- 15:51
  5. */
  6. public class DemoTest {
  7. public static void main(String[] args) {
  8. int a=100;
  9. int b=33;
  10. double f1 = new BigDecimal((float)a/b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  11. System.out.println(f1);
  12. }
  13. }

第二种

  1. import java.text.DecimalFormat;
  2. /**
  3. * @author WGR
  4. * @create 2020/3/17 -- 15:51
  5. */
  6. public class DemoTest {
  7. public static void main(String[] args) {
  8. int a=100;
  9. int b=33;
  10. DecimalFormat df = new DecimalFormat("0.00");//格式化小数
  11. String num = df.format((float)a/b);//返回的是String类型
  12. System.out.println(num);
  13. }
  14. }