网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Lambda表达式mapToDouble.sum精度问题

public static void main(String[] args) {
        List doubles = Arrays.asList(new Double(3.3), new Double(3.3), new Double(3.3));
        double sum = doubles.stream().mapToDouble(Double::doubleValue).sum();
        System.out.println(sum);// 9.899999999999999
        System.out.println(formatDigit(sum,2));// 9.9
        System.out.println(formatDigit_down(sum, 2)); // 9.89
        List doubles1 = Arrays.asList(new Double(1.1), new Double(1.1), new Double(1.1));
        double sum1 = doubles1.stream().mapToDouble(Double::doubleValue).sum();
        System.out.println(sum1);// 3.3000000000000003
        System.out.println(formatDigit(sum1,2));// 3.3
        System.out.println(formatDigit_down(sum1, 2));// 3.3
    }
    /**
     * 将数据转换为保留指定小数位数(0,1,2)格式的数,四舍五入
     *
     */
    public static double formatDigit(double num, int decimalPlace) {
        DecimalFormat fm = null;
        switch (decimalPlace) {
            case 0:
                fm = new DecimalFormat("##");
                break;
            case 1:
                fm = new DecimalFormat("##.#");
                break;
            case 2:
                fm = new DecimalFormat("##.##");
                break;
            case 3:
                fm = new DecimalFormat("##.###");
                break;
            case 4:
                fm = new DecimalFormat("##.####");
                break;
            default:
                break;
        }

        if (fm == null) {
            return num;
        }

        StringBuffer sbf = new StringBuffer();
        fm.format(num, sbf, new FieldPosition(java.text.NumberFormat.FRACTION_FIELD));
        return Double.parseDouble(sbf.toString());
    }

    /**
     * 将数据转换为保留指定小数位数(0,1,2)格式的数。向下取值:如 5.567 -> 5.56 -5.567 -> -5.56
     */
    public static double formatDigit_down(double num, int decimalPlace) {
        DecimalFormat fm = null;
        switch (decimalPlace) {
            case 0:
                fm = new DecimalFormat("##");
                break;
            case 1:
                fm = new DecimalFormat("##.#");
                break;
            case 2:
                fm = new DecimalFormat("##.##");
                break;
            case 3:
                fm = new DecimalFormat("##.###");
                break;
            case 4:
                fm = new DecimalFormat("##.####");
                break;
            default:
                break;
        }

        if (fm == null) {
            return num;
        }

        StringBuffer sbf = new StringBuffer();
        fm.setRoundingMode(RoundingMode.DOWN);// 向下取值设置、
        fm.format(num, sbf, new FieldPosition(java.text.NumberFormat.FRACTION_FIELD));
        return Double.parseDouble(sbf.toString());
    }

网页题目:Lambda表达式mapToDouble.sum精度问题
分享URL:http://bjjierui.cn/article/ggpjhp.html

其他资讯