尽量用字符串的形式初始化
加法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.add(twoValue);
System.out.println("加法用value结果 = " + resultValue);
// 加法用value结果 = 1000000.005000000000000000104083408558608425664715468883514404296875
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.add(twoString);
System.out.println("加法用string结果 = " + resultString);
// 加法用string结果 = 1000000.005
减法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.subtract(twoValue);
System.out.println("减法用value结果 = " + resultValue);
// 减法用value结果 = -999999.994999999999999999895916591441391574335284531116485595703125
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.subtract(twoString);
System.out.println("减法用string结果 = " + resultString);
// 减法用string结果 = -999999.995
乘法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.multiply(twoValue);
System.out.println("乘法用value结果 = " + resultValue);
// 乘法用value结果 = 5000.000000000000104083408558608425664715468883514404296875000000
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.multiply(twoString);
System.out.println("乘法用string结果 = " + resultString);
// 乘法用string结果 = 5000.000
除法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.divide(twoValue);
System.out.println("除法用value结果 = " + resultValue);
// 除法用value结果 = 5.000000000000000104083408558608425664715468883514404296875E-9
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.divide(twoString);
System.out.println("除法用string结果 = " + resultString);
// 除法用string结果 = 5E-9
除法精度
// 被除数
BigDecimal dividend = new BigDecimal("1");
// 除数
BigDecimal divisor = new BigDecimal("3");
//                                除数    精确小数位   舍入模式
BigDecimal res1 = dividend.divide(divisor,3,BigDecimal.ROUND_UP);
System.out.println("除法ROUND_UP:"+res1);
// 除法ROUND_UP:0.334
BigDecimal res2 = dividend.divide(divisor,3,BigDecimal.ROUND_DOWN);
System.out.println("除法ROUND_DOWN:"+res2);
// 除法ROUND_DOWN:0.333
BigDecimal res3 = dividend.divide(divisor,3,BigDecimal.ROUND_CEILING);
System.out.println("除法ROUND_CEILING:"+res3);
// 除法ROUND_CEILING:0.334
BigDecimal res4 = dividend.divide(divisor,3,BigDecimal.ROUND_FLOOR);
System.out.println("除法ROUND_FLOOR:"+res4);
// 除法ROUND_FLOOR:0.333
BigDecimal res5 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_UP);
System.out.println("除法ROUND_HALF_UP:"+res5);
// 除法ROUND_HALF_UP:0.333
BigDecimal res6 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_DOWN);
System.out.println("除法ROUND_HALF_DOWN:"+res6);
// 除法ROUND_HALF_DOWN:0.333
BigDecimal res7 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_EVEN);
System.out.println("除法ROUND_HALF_EVEN:"+res7);
// 除法ROUND_HALF_EVEN:0.333
BigDecimal res8 = dividend.divide(divisor,3,BigDecimal.ROUND_UNNECESSARY);
System.out.println("除法ROUND_UNNECESSARY:"+res8);
// java.lang.ArithmeticException: Rounding necessary
根据给定的舍入模式将数字舍入为一位数的结果
| Input Number | 
UP | 
DOWN | 
CEILING | 
FLOOR | 
HALF_UP | 
HALF_DOWN | 
HALF_EVEN | 
UNNECESSARY | 
| 5.5 | 
6 | 
5 | 
6 | 
5 | 
6 | 
5 | 
6 | 
throw ArithmeticException | 
| 2.5 | 
3 | 
2 | 
3 | 
2 | 
3 | 
2 | 
2 | 
throw ArithmeticException | 
| 1.6 | 
2 | 
1 | 
2 | 
1 | 
2 | 
2 | 
2 | 
throw ArithmeticException | 
| 1.1 | 
2 | 
1 | 
2 | 
1 | 
1 | 
1 | 
1 | 
throw ArithmeticException | 
| 1.0 | 
1 | 
1 | 
1 | 
1 | 
1 | 
1 | 
1 | 
1 | 
| -1.0 | 
-1 | 
-1 | 
-1 | 
-1 | 
-1 | 
-1 | 
-1 | 
-1 | 
| -1.1 | 
-2 | 
-1 | 
-1 | 
-2 | 
-1 | 
-1 | 
-1 | 
throw ArithmeticException | 
| -1.6 | 
-2 | 
-1 | 
-1 | 
-2 | 
-2 | 
-2 | 
-2 | 
throw ArithmeticException | 
| -2.5 | 
-3 | 
-2 | 
-2 | 
-3 | 
-3 | 
-2 | 
-2 | 
throw ArithmeticException | 
| -5.5 | 
-6 | 
-5 | 
-5 | 
-6 | 
-6 | 
-5 | 
-6 | 
throw ArithmeticException | 
BigDecimal转其它类型
- 一般来说,可以使用BigDecimal的构造方法或者静态方法的valueOf()方法把基本类型的变量构建成BigDecimal对象  BigDecimal.valueOf(0.48);
 
// double 转 String
BigDecimal doubleString = new BigDecimal(Double.toString(0.5));
String s = doubleString.toString();
System.out.println("s = " + s);
// s = 0.5
// int 转 String
BigDecimal intString = new BigDecimal(Integer.toString(1000));
String i = intString.toString();
System.out.println("i = " + i);
// i = 1000
// float 转 String
BigDecimal floatString = new BigDecimal(Float.toString(0.5f));
String f = floatString.toString();
System.out.println("f = " + f);
// f = 0.5
// BigDecimal 转 double
double dv = doubleString.doubleValue();
System.out.println("dv = " + dv);
// dv = 0.5
// BigDecimal 转 int
int iv = intString.intValue();
System.out.println("iv = " + iv);
// iv = 1000
// BigDecimal 转 float
float fv = floatString.floatValue();
System.out.println("fv = " + fv);
// fv = 0.  5