初始化
BigDecimal amount = new BigDecimal("15");
加减乘除
public static void main(String[] args) { BigDecimal amount = new BigDecimal("15"); BigDecimal amount2 = new BigDecimal("3"); //加 BigDecimal add = amount.add(amount2); System.out.println(add); //减 BigDecimal subtract = amount.subtract(amount2); System.out.println(subtract); //乘 BigDecimal multiply = amount.multiply(amount2); System.out.println(multiply); //除 BigDecimal divide = amount.divide(amount2); System.out.println(divide); }
比较大小
BigDecimal amount = new BigDecimal("15"); BigDecimal amount2 = new BigDecimal("3"); //-1表示小于,0是等于,1是大于 int i = amount.compareTo(amount2); System.out.println(i);
除以100并保留两位小数
BigDecimal amount = new BigDecimal("15"); BigDecimal amount2 = new BigDecimal("3"); BigDecimal divide2 = amount.divide(new BigDecimal("100")).setScale(2); System.out.println(divide2);
如果不需要保留小数,则setScale设置为0即可。
标签:基本,BigDecimal,amount2,System,用法,amount,new,out From: https://www.cnblogs.com/zys2019/p/18544667