BigDecimal 转成 double

NUMBER(20,2)

数据库里的字段number  ,实体是BigDecimal

将BigDecimal转成double

public double getOrderamount() {

        if (orderamount != null) {

            BigDecimal b2 = new BigDecimal(100);

            return orderamount.divide(b2, 2, BigDecimal.ROUND_HALF_DOWN).doubleValue();

        }else{

          return 0.00;// orderamount数据库里为null的话, //orderamount.doubleValue();就会报错,NULL是转换不了double类型的

         //return orderamount.doubleValue();

        }

说明:::::::::::::

BigDecimal.divide();除法

MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);//指定精度
//精度为2,舍入模式为大于0.5进1,否则舍弃。
BigDecimal a = new BigDecimal(0.5);
BigDecimal b = new BigDecimal(0.2);
System.out.println(a.divide(b,mc));//输出结果是2.5,如果没有指定精度的话,divide会报错

错误信息:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
posted @ 2015-08-11 14:37  hanxue1122  阅读(3723)  评论(0编辑  收藏  举报