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

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