尽量用字符串的形式初始化
加法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.add(twoValue);
System.out.println("加法用value结果 = " + resultValue);
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.add(twoString);
System.out.println("加法用string结果 = " + resultString);
减法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.subtract(twoValue);
System.out.println("减法用value结果 = " + resultValue);
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.subtract(twoString);
System.out.println("减法用string结果 = " + resultString);
乘法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.multiply(twoValue);
System.out.println("乘法用value结果 = " + resultValue);
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.multiply(twoString);
System.out.println("乘法用string结果 = " + resultString);
除法
BigDecimal oneValue = new BigDecimal(0.005);
BigDecimal twoValue = new BigDecimal(1000000);
BigDecimal resultValue = oneValue.divide(twoValue);
System.out.println("除法用value结果 = " + resultValue);
BigDecimal oneString = new BigDecimal("0.005");
BigDecimal twoString = new BigDecimal("1000000");
BigDecimal resultString = oneString.divide(twoString);
System.out.println("除法用string结果 = " + resultString);
除法精度
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);
BigDecimal res2 = dividend.divide(divisor,3,BigDecimal.ROUND_DOWN);
System.out.println("除法ROUND_DOWN:"+res2);
BigDecimal res3 = dividend.divide(divisor,3,BigDecimal.ROUND_CEILING);
System.out.println("除法ROUND_CEILING:"+res3);
BigDecimal res4 = dividend.divide(divisor,3,BigDecimal.ROUND_FLOOR);
System.out.println("除法ROUND_FLOOR:"+res4);
BigDecimal res5 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_UP);
System.out.println("除法ROUND_HALF_UP:"+res5);
BigDecimal res6 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_DOWN);
System.out.println("除法ROUND_HALF_DOWN:"+res6);
BigDecimal res7 = dividend.divide(divisor,3,BigDecimal.ROUND_HALF_EVEN);
System.out.println("除法ROUND_HALF_EVEN:"+res7);
BigDecimal res8 = dividend.divide(divisor,3,BigDecimal.ROUND_UNNECESSARY);
System.out.println("除法ROUND_UNNECESSARY:"+res8);
根据给定的舍入模式将数字舍入为一位数的结果
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);
BigDecimal doubleString = new BigDecimal(Double.toString(0.5));
String s = doubleString.toString();
System.out.println("s = " + s);
BigDecimal intString = new BigDecimal(Integer.toString(1000));
String i = intString.toString();
System.out.println("i = " + i);
BigDecimal floatString = new BigDecimal(Float.toString(0.5f));
String f = floatString.toString();
System.out.println("f = " + f);
double dv = doubleString.doubleValue();
System.out.println("dv = " + dv);
int iv = intString.intValue();
System.out.println("iv = " + iv);
float fv = floatString.floatValue();
System.out.println("fv = " + fv);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?