概述
package com.ylchou.testBigDecimal;
import java.math.BigDecimal;
public class TestBigDecimal {
public static void main(String[] args) {
System.out.println(0.08+0.07);
System.out.println(0.08-0.07);
System.out.println(0.08*0.07);
System.out.println(0.08/0.07);
// System.out.println(0.05-0.02);
// System.out.println(1.15*100);
// System.out.println(123.3/100);
System.out.println("--------------------------------------------");
BigDecimal d1 = new BigDecimal(0.08);
BigDecimal d2 = new BigDecimal(0.07);
System.out.println(d1.add(d2));
System.out.println(d1.subtract(d2));
System.out.println(d1.multiply(d2));
// System.out.println(d1.divide(d2));//异常
System.out.println(d1.divide(d2,10,BigDecimal.ROUND_HALF_UP));
System.out.println("--------------------------------------------");
BigDecimal d11 = new BigDecimal("0.08");
BigDecimal d22 = new BigDecimal("0.07");
System.out.println(d11.add(d22));
System.out.println(d11.subtract(d22));
System.out.println(d11.multiply(d22));
// System.out.println(d11.divide(d22));//异常
System.out.println(d11.divide(d22,10,BigDecimal.ROUND_HALF_UP));
System.out.println("--------------------------------------------");
BigDecimal d111 = BigDecimal.valueOf(0.08);
BigDecimal d222 = BigDecimal.valueOf(0.07);
System.out.println(d111.add(d222));
System.out.println(d111.subtract(d222));
System.out.println(d111.multiply(d222));
// System.out.println(d111.divide(d222));//异常
System.out.println(d111.divide(d222,10,BigDecimal.ROUND_HALF_UP));
System.out.println("--------------------------------------------");
}
}
控制台输出:
0.15000000000000002
0.009999999999999995
0.005600000000000001
1.1428571428571428
--------------------------------------------
0.15000000000000000832667268468867405317723751068115234375
0.00999999999999999500399638918679556809365749359130859375
0.00560000000000000064948046940571658724118100550360839641493294928918135477946549372063600458204746246337890625
1.1428571429
--------------------------------------------
0.15
0.01
0.0056
1.1428571429
--------------------------------------------
0.15
0.01
0.0056
1.1428571429
--------------------------------------------
最后
以上就是包容皮带为你收集整理的Java类BigDecimal(1)的全部内容,希望文章能够帮你解决Java类BigDecimal(1)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复