Java基础之“Double类型,保留两位小数,避免因小数过长导致乱码问题”

保留两位小数

方案一

        Double totalSaleAmount = 4.174558045899999E7;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("df.format(totalSaleAmount) = " +     df.format(totalSaleAmount)); 

方案二

Double totalSaleAmount = 4.174558045899999E7;
NumberFormat percentInstance = NumberFormat.getPercentInstance();
percentInstance.setMinimumFractionDigits(2);
Double a = percentInstance.format(totalSaleamount);
System.out.println(a);

方案三


Double totalSaleAmount = 4.174558045899999E7;
System.out.println(new DecimalFormat("0.00").format(totalSaleAmount));

 

结果图

 

posted @ 2023-06-21 09:15  骚哥  阅读(139)  评论(0编辑  收藏  举报