import java
.math
.BigDecimal
;
public class Demo1 {
public static void main(String
[] args
) {
BigDecimal bigDecimal
= new BigDecimal(54.568);
BigDecimal bigDecimal1
= new BigDecimal(30.151);
BigDecimal bigDecimal2
= new BigDecimal(54.565);
BigDecimal bigDecimal6
= new BigDecimal("3");
BigDecimal add
= bigDecimal
.add(bigDecimal1
);
System
.out
.println(add
);
BigDecimal add1
= bigDecimal
.add(bigDecimal1
).setScale(2, BigDecimal
.ROUND_DOWN);
System
.out
.println(add1
);
BigDecimal add2
= bigDecimal
.add(bigDecimal1
).setScale(2,BigDecimal
.ROUND_UP);
System
.out
.println(add2
);
BigDecimal add3
= bigDecimal
.add(bigDecimal1
).setScale(2, BigDecimal
.ROUND_HALF_UP);
System
.out
.println(add3
);
BigDecimal add4
= bigDecimal
.add(bigDecimal1
.setScale(2, BigDecimal
.ROUND_HALF_DOWN));
System
.out
.println(add4
);
BigDecimal bigDecimal3
= bigDecimal2
.setScale(2, BigDecimal
.ROUND_HALF_DOWN);
System
.out
.println(bigDecimal3
);
System
.out
.println("=========================");
BigDecimal subtract
= bigDecimal
.subtract(bigDecimal1
);
System
.out
.println(subtract
);
BigDecimal subtract1
= bigDecimal
.subtract(bigDecimal1
).setScale(2, BigDecimal
.ROUND_DOWN);
System
.out
.println(subtract1
);
BigDecimal subtract2
= bigDecimal
.subtract(bigDecimal1
).setScale(2, BigDecimal
.ROUND_UP);
System
.out
.println(subtract2
);
BigDecimal subtract3
= bigDecimal
.subtract(bigDecimal1
).setScale(2, BigDecimal
.ROUND_HALF_UP);
System
.out
.println(subtract3
);
System
.out
.println("====================");
BigDecimal multiply
= bigDecimal
.multiply(bigDecimal1
);
System
.out
.println(multiply
);
BigDecimal multiply1
= bigDecimal
.multiply(bigDecimal1
).setScale(2, BigDecimal
.ROUND_DOWN);
System
.out
.println(multiply1
);
BigDecimal multiply2
= bigDecimal
.multiply(bigDecimal1
).setScale(2, BigDecimal
.ROUND_UP);
System
.out
.println(multiply2
);
BigDecimal multiply3
= bigDecimal
.multiply(bigDecimal1
).setScale(2, BigDecimal
.ROUND_HALF_UP);
System
.out
.println(multiply3
);
System
.out
.println("==============");
BigDecimal divide
= bigDecimal
.divide(bigDecimal6
,2,BigDecimal
.ROUND_HALF_UP);
System
.out
.println(divide
);
BigDecimal divide1
= bigDecimal
.divide(bigDecimal6
,3,BigDecimal
.ROUND_HALF_UP);
System
.out
.println(divide1
);
BigDecimal divide2
= bigDecimal
.divide(bigDecimal6
, 2, BigDecimal
.ROUND_DOWN);
System
.out
.println(divide2
);
}
}
(1) 问题
String string
= null;
BigDecimal b1
= new BigDecimal(string
);
BigDecimal b2
= new BigDecimal("9");
BigDecimal a
= b1
.add(b2
);
System
.out
.println(a
);
DecimalFormat 数字格式化
Integer i
= 1;
DecimalFormat decimalFormat
= new DecimalFormat("000");
String format
= decimalFormat
.format(i
);
System
.out
.println(format
);
转载请注明原文地址: https://yun.8miu.com/read-18105.html