import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Main { public static Scanner cin = new Scanner(System.in); public static void main(String[] args) { BigDecimal a = new BigDecimal("4.51"); BigDecimal b = new BigDecimal("1.5"); System.out.println("a + b =" + a.add(b)); System.out.println("a - b =" + a.subtract(b)); System.out.println("a * b =" + a.multiply(b)); //下面的不能整除,会报错,可以指定保留小数即可 // System.out.println("a / b =" + a.divide(b)); //第二个参数表示小数点后保留位数 System.out.println("a / b =" + a.divide(b,1)); System.out.println("a / b =" + a.divide(b,2)); } }